在下面的代码中,我试图打开一系列文本文件并将它们的内容复制到一个文件中。我在“os.write(out_file,line)”上遇到错误,它要求我输入一个整数。我还没有定义什么是“线”,那是问题吗?我是否需要以某种方式指定“行”是 in_file 中的文本字符串?此外,我通过 for 循环的每次迭代打开 out_file。那不好吗?我应该在开始时打开一次吗?谢谢!
import os
import os.path
import shutil
# This is supposed to read through all the text files in a folder and
# copy the text inside to a master file.
# This defines the master file and gets the source directory
# for reading/writing the files in that directory to the master file.
src_dir = r'D:\Term Search'
out_file = r'D:\master.txt'
files = [(path, f) for path,_,file_list in os.walk(src_dir) for f in file_list]
# This for-loop should open each of the files in the source directory, write
# their content to the master file, and finally close the in_file.
for path, f_name in files:
open(out_file, 'a+')
in_file = open('%s/%s' % (path, f_name), 'r')
for line in in_file:
os.write(out_file, line)
close(file_name)
close(out_file)
print 'Finished'