我有100个文本文件,我想将它保存到100个文本文件中。现在,我的编码可以读取所有文件,但它只保存一个文件,这是最新的结果。在这里,我附上了代码。
def nama():
path = "C:/Amar/code/"
infilename = os.listdir(path)
print len(infilename)
for filename in infilename:
print("jumpa dah" + path + "\\"+ filename)
f = open(path + "\\" + filename, "r")
data = f.read()
f.close()
lines = data.split('\n')
outfilename = path + "result.txt"
print outfilename
f = open(outfilename , "a")
答案 0 :(得分:1)
附加一个字符串,作为每个输出文件的唯一标识符。您可以使用输入文件名:
outfilename = path + filename + "_result.txt"
# e.g reports_result.txt