input输入多行文本,保存为.dot文件和对应的.txt文件

需求

复制代码
不管是上面的dot还是这个dot
变成
input输入文本
 按"# ꧂ ꧁"结束
 保存在dot文本文件夹下,

用txt保存每个文件文件名:
编号. 第二行有字文字  时间戳 

代码

首先,我会创建一个Python脚本,它将接受用户的输入,直到输入以# ꧂ ꧁结束,然后将输入保存为.dot文件和对应的.txt文件。代码如下:

python 复制代码
import os
from datetime import datetime

def save_input_to_files():
    input_text = ""
    file_number = 1

    while True:
        line = input().strip()
        if line == "# ꧂ ꧁":
            break
        input_text += line + "\n"

    # 生成时间戳
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    # 写入到dot文件中
    dot_filename = f"{file_number}.dot"
    with open(dot_filename, "w") as dot_file:
        dot_file.write(input_text)

    # 写入到txt文件中
    txt_filename = f"{file_number}.txt"
    with open(txt_filename, "w") as txt_file:
        txt_file.write(f"File Number: {file_number}\n")
        txt_file.write(f"Timestamp: {timestamp}\n")
        txt_file.write(input_text)

    print(f"Input saved to {dot_filename} and {txt_filename}")

    file_number += 1

if __name__ == "__main__":
    save_input_to_files()
相关推荐
Csvn18 小时前
🌟 LangChain 30 天保姆级教程 · Day 13|OutputParser 进阶!让 AI 输出自动转为结构化对象,并支持自动重试!
python·langchain
cch891818 小时前
Python主流框架全解析
开发语言·python
sg_knight19 小时前
设计模式实战:状态模式(State)
python·ui·设计模式·状态模式·state
好运的阿财19 小时前
process 工具与子agent管理机制详解
网络·人工智能·python·程序人生·ai编程
张張40819 小时前
(域格)环境搭建和编译
c语言·开发语言·python·ai
weixin_4235339919 小时前
【Windows11离线安装anaconda、python、vscode】
开发语言·vscode·python
Ricky111zzz19 小时前
leetcode学python记录1
python·算法·leetcode·职场和发展
小白学大数据20 小时前
Selenium+Python 爬虫:动态加载头条问答爬取
爬虫·python·selenium
Hui Baby20 小时前
springboot读取配置文件
后端·python·flask
阿Y加油吧20 小时前
回溯法经典难题:N 皇后问题 深度解析 + 二分查找入门(搜索插入位置)
开发语言·python