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()
相关推荐
AI探索者7 小时前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者7 小时前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh9 小时前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅9 小时前
Python函数入门详解(定义+调用+参数)
python
曲幽10 小时前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时13 小时前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿16 小时前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户8356290780511 天前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng81 天前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi1 天前
Chapter 2 - Python中的变量和简单的数据类型
python