import os
#reads all the files and put everything in data
number_of_files = 0
data =[]
for i in range (number_of_files):
fn = os.path.join(os.path.dirname(__file__), 'testing/file_%d.txt' % i)
f = open(fn, 'r')
for line in f:
data.append(line)
f.close()
#write everything to result.txt
fn = os.path.join(os.path.dirname(__file__), 'result.txt')
f = open(fn, 'w')
for element in data:
f.write(element)
f.close()
import glob, os
os.chdir('/root')
with open('result.txt', 'w+') as result_file:
for filename in glob.glob('result*.txt'):
with open(filename) as file:
result_file.write(file.read())
# append a line break if you want to separate them
result_file.write("\n")
这可能是一个简单的方法
比如说我的文件脚本.py在一个文件夹中,与该脚本一起有一个名为testing的文件夹,其中包含所有文本文件,如file\u 0,file\u 1。。。。你知道吗
正如其他人所建议的,使用cat可能更容易。如果必须用Python来完成,这应该是可行的。它查找目录中的所有文本文件,并将其内容附加到结果文件中。你知道吗
相关问题 更多 >
编程相关推荐