指定文件夹中的txt文件每一行后面写入
import sys, string, os, shutil
srcfiles = os.listdir("/home/")
xies = "/home/"
yuse ="/home/"
# print(srcfiles)
for srcfile in srcfiles:
print(srcfile)
ff = open(xies +"//"+srcfile, 'w') # 打开一个文件,可写模式
with open(yuse+"//"+srcfile, 'r') as f: # 打开一个文件只读模式
line = f.readlines()# 读取文件中的每一行,放入line列表中
for line_list in line:
line_new = line_list.replace('\n', '')# 将换行符替换为空('')
line_new = line_new + r' 1' + '\n' # 行末尾加上"|",同时加上"\n"换行符
print(line_new)
ff.write(line_new) # 写入一个新文件中