Python 实现写 txt 文件:按不同条件在指定文件夹操作古诗内容
name = "李小龙" def write_files(c=1): folder_path = f"/data/share_files/experiment_file/e_15915/datafiles/{name}" file_path = f"{folder_path}/write.txt" if c == 1: with open(file_path, 'w') as file: file.write("白日依山尽,\n黄海入海流。\n") elif c == 2: with open(file_path, 'a') as file: file.write("更上一层楼。\n") elif c == 3: with open(file_path, 'r') as file: lines = file.readlines() lines.insert(2, "欲穷千里目,\n") lines.insert(0, "作者:王之涣\n登鹳雀楼\n") with open(file_path, 'w') as file: for line in lines: file.write(line) # 测试调用 write_files(1) write_files(2) write_files(3) ######[AI写代码神器 | 503点数解答 | 2024-11-26 11:58:08]
- 批处理文件:依据参数表.xlsx精准修改文件.txt中p、t、s值(GPT | 811点数解答 | 2024-12-05 12:58:37)401
- 用批处理和Python实现“参数表.xlsx”与“文件.txt”数据匹配及修改(讯飞星火 | 531点数解答 | 2024-12-05 13:20:42)381
- 批处理文件:依据参数表.xlsx精准修改文件.txt特定参数值(阿里通义 | 495点数解答 | 2024-12-05 13:22:26)358
- 测3:以读、二进制方式打开C盘temp文件夹下abc.txt文件的代码选哪个?(GPT | 15点数解答 | 2024-06-06 13:53:37)410
- Python实现Q老师成绩管理任务:修改与查看学生成绩(字节豆包 | 191点数解答 | 2025-12-20 20:55:41)90
- Python 代码助力 Q 老师高效管理班级学生成绩:修改与查看一键搞定(字节豆包 | 326点数解答 | 2025-12-27 20:48:12)99
- 2025 暑假苍溪 - 广安 - 重庆红色研学:中小学生传承红色基因之旅 (字节豆包 | 1244点数解答 | 2025-06-19 16:32:34)248
- Python 运行报错:urllib3 网络连接创建失败,原因及排查建议(字节豆包 | 161点数解答 | 2024-10-31 00:39:36)479
- Linux 知识问答:判断文件 test.txt 是否存在的正确命令揭晓!(字节豆包 | 62点数解答 | 2024-11-22 17:14:08)297
- Python 实现将当前目录文件名写入 url.txt,附三大实用改进建议!(字节豆包 | 127点数解答 | 2024-12-25 11:19:25)245
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)362
- Python 实现将当前目录文件名写入 url.txt 及 3 点代码改进建议 (字节豆包 | 130点数解答 | 2024-12-25 11:15:01)331