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()
相关推荐
极梦网络无忧7 小时前
OpenClaw 基础使用说明(中文版)
python
hz_zhangrl7 小时前
CCF-GESP 等级考试 2026年3月认证C++六级真题解析
青少年编程·程序设计·gesp·c++六级·gesp c++·gesp c++六级·gesp2026年3月
codeJinger7 小时前
【Python】操作Excel文件
python·excel
XLYcmy8 小时前
一个针对医疗RAG系统的数据窃取攻击工具
python·网络安全·ai·llm·agent·rag·ai安全
Islucas8 小时前
Claude code入门保姆级教程
python·bash·claude
萝卜白菜。8 小时前
TongWeb7.0相同的类指明加载顺序
开发语言·python·pycharm
赵钰老师8 小时前
【ADCIRC】基于“python+”潮汐、风驱动循环、风暴潮等海洋水动力模拟实践技术应用
python·信息可视化·数据分析
爬山算法9 小时前
MongoDB(80)如何在MongoDB中使用多文档事务?
数据库·python·mongodb
YuanDaima20489 小时前
基于 LangChain 1.0 的检索增强生成(RAG)实战
人工智能·笔记·python·langchain·个人开发·langgraph