我有一个20个文件名的列表,比如['file1.txt', 'file2.txt',…]。我想写一个Python脚本将这些文件连接到一个新文件中。我可以通过f = open(…)打开每个文件,通过调用f.r edline()逐行读取,并将每行写入新文件。这对我来说似乎不是很“优雅”,尤其是我必须一行一行地读/写的部分。
在Python中是否有更“优雅”的方式来做到这一点?
我有一个20个文件名的列表,比如['file1.txt', 'file2.txt',…]。我想写一个Python脚本将这些文件连接到一个新文件中。我可以通过f = open(…)打开每个文件,通过调用f.r edline()逐行读取,并将每行写入新文件。这对我来说似乎不是很“优雅”,尤其是我必须一行一行地读/写的部分。
在Python中是否有更“优雅”的方式来做到这一点?
当前回答
@inspectorG4dget答案的替代答案(到2016年3月29日为止的最佳答案)。我测试了3个436MB的文件。
@inspectorG4dget答案:162秒
解决方案:125秒
from subprocess import Popen
filenames = ['file1.txt', 'file2.txt', 'file3.txt']
fbatch = open('batch.bat','w')
str ="type "
for f in filenames:
str+= f + " "
fbatch.write(str + " > file4results.txt")
fbatch.close()
p = Popen("batch.bat", cwd=r"Drive:\Path\to\folder")
stdout, stderr = p.communicate()
其思想是创建一个批处理文件并执行它,利用“旧的好技术”。它是半python,但运行速度更快。适用于windows。
其他回答
这样就行了
对于大文件:
filenames = ['file1.txt', 'file2.txt', ...]
with open('path/to/output/file', 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
for line in infile:
outfile.write(line)
对于小文件:
filenames = ['file1.txt', 'file2.txt', ...]
with open('path/to/output/file', 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
outfile.write(infile.read())
我还想到了另一个有趣的问题:
filenames = ['file1.txt', 'file2.txt', ...]
with open('path/to/output/file', 'w') as outfile:
for line in itertools.chain.from_iterable(itertools.imap(open, filnames)):
outfile.write(line)
遗憾的是,最后一个方法留下了一些打开的文件描述符,无论如何GC都应该处理这些描述符。我只是觉得很有趣
这正是fileinput的作用:
import fileinput
with open(outfilename, 'w') as fout, fileinput.input(filenames) as fin:
for line in fin:
fout.write(line)
对于这个用例,它实际上并不比手动遍历文件简单多少,但在其他情况下,使用单个迭代器遍历所有文件,就像它们是单个文件一样非常方便。(此外,fileinput在完成后立即关闭每个文件的事实意味着不需要使用或关闭每个文件,但这只是节省一行,不是什么大不了的事情。)
在fileinput中还有一些其他漂亮的特性,比如通过过滤每一行来对文件进行就地修改的能力。
正如评论中所指出的,并在另一篇文章中讨论,Python 2.7的文件输入将无法正常工作。这里略有修改,使代码符合Python 2.7
with open('outfilename', 'w') as fout:
fin = fileinput.input(filenames)
for line in fin:
fout.write(line)
fin.close()
outfile.write(infile.read()) # time: 2.1085190773010254s
shutil.copyfileobj(fd, wfd, 1024*1024*10) # time: 0.60599684715271s
一个简单的基准测试表明,shutil的性能更好。
如果文件不是很大:
with open('newfile.txt','wb') as newf:
for filename in list_of_files:
with open(filename,'rb') as hf:
newf.write(hf.read())
# newf.write('\n\n\n') if you want to introduce
# some blank lines between the contents of the copied files
如果文件太大,不能完全读取并保存在RAM中,则算法必须稍微不同,以固定长度的块读取循环中复制的每个文件,例如使用read(10000)。
import os
files=os.listdir()
print(files)
print('#',tuple(files))
name=input('Enter the inclusive file name: ')
exten=input('Enter the type(extension): ')
filename=name+'.'+exten
output_file=open(filename,'w+')
for i in files:
print(i)
j=files.index(i)
f_j=open(i,'r')
print(f_j.read())
for x in f_j:
outfile.write(x)