酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

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]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]