我有一个关于如何从100个文件中添加条目(每个文件包含两列)然后将它们写入新文件(也包含两列)的问题?
答案 0 :(得分:0)
你想链接他们吗?即,你想要文件1的所有行,然后是文件2的所有行,...... 或者你想合并它们?文件1的第1行,文件2的第1行,......
对于第一种情况:
from itertools import chain
filenames = ...
file_handles = [open(fn) for fn in filenames]
with open("output.txt", "w") as out_fh:
for line in chain(file_handles):
out_fh.write(line)
for fh in file_handles:
fh.close()
对于第二种情况:
from itertools import izip_longest
filenames = ...
file_handles = [open(fn) for fn in filenames]
with open("output.txt", "w") as out_fh:
for lines in izip_longest(*file_handles, fillvalue=None):
for line in lines:
if line is not None:
out_fh.write(line)
for fh in file_handles:
fh.close()
重要提示:永远不要忘记关闭文件!
正如@isedev所指出的,这种方法是o.k.对于100个文件,但是当我立即打开所有句柄时,对于数千个文件,这将无效。
如果你想克服这个问题,只有选项1(链接)是合理的......
filenames = ...
with open("output.txt", "w") as out_fh:
for fn in filenames:
with open(fn) as fh:
for line in fh:
out_fh.write(line)
答案 1 :(得分:0)
这是非常不明确的。目前尚不清楚你的问题是什么。
可能你会做类似的事情:
entries = []
for f in ["file1.txt", "file2.txt", ..., "file100.txt"]:
entries.append(open(f).readlines())
o = open("output.txt", "w")
o.writelines(entries)
o.close()
答案 2 :(得分:0)
不确定您是否需要一个解决方案来查找所有这100个文件? 如果是这样,这里有一种方法,包括全部读取并将它们写入连接文件:
from os import walk
from os.path import abspath
lines = []
for root, folders, files in walk('./path/'):
for file in files:
fh = open(abspath(root + '/' + file), 'rb')
lines.append(fh.read())
fh.close()
# break if you only want the first level of your directory tree
o = open('output.txt', 'wb')
o.write('\n'.join(lines))
o.close()
您还可以执行“内存效率高”解决方案:
from os import walk
from os.path import abspath
o = open('output.txt', 'wb')
for root, folders, files in walk('./path/'):
for file in files:
fh = open(abspath(root + '/' + file), 'rb')
for line in fh.readline():
o.write(line)
del line
fh.close()
del fh
# break if you only want the first level of your directory tree
o.close()
其中大部分是自动化的(我思考)在Python中,但是懒惰与否,如果你可以在关闭文件后以及在重用变量名之前和之前从内存中删除对象..只是以防?
答案 3 :(得分:0)
一种更具伸缩性的方式,受到Torxed方法的启发
from os import walk
from os.path import abspath
with open('output.txt', 'wb') as o:
for root, folders, files in walk('./path/'):
for filename in files:
with open(abspath(root + '/' + filename), 'rb') as i:
for line in i:
o.write(line)