python批量添加多个txt文件前缀


import os

# 原txt文件路径
txt_file_path = r'D:\File\文本'
# 修改后的txt文件路径
save_file_path = r'D:\File\新建文件夹'

total_txt = os.listdir(txt_file_path)
num = len(total_txt)


s = """ 
这里是需要添加的内容.
"""

i = 0
for each in total_txt:
    with open(txt_file_path+'/'+str(each), 'r', encoding='utf-8') as f:
        data = f.readlines()
        data.insert(0, s)
        name = save_file_path+'/'+str(each)
        with open(name, 'w') as f:
            f.writelines(data)
            i += 1
            print('第{}个txt写入成功'.format(i))

你可能感兴趣的:(Python自动化办公,python)