import os def Line_Add_File(file, str): file_data = "" with open(file, "r") as f: for line in f: line = line.replace("\n",'') + str + "\n" # 因为存储时候拼接字符串会出现换行,所以需要去掉末尾的换行符 file_data = file_data + line with open(file, "w") as f: f.write(file_data) base_dir = "C://Users//49115//Desktop//test//" str = "1" for root, dirs, files in os.walk(base_dir):#文档内所有文件 for file_name in files: #文件夹下各文件名(包括子文件中) if file_name.endswith('.txt'):#要修改的文件类型 filename = os.path.join(root, file_name) print(filename) Line_Add_File(filename, str)
python实现批量TXT文档每段结尾添加字符
最新推荐文章于 2023-09-02 14:20:30 发布