如何在 Python 中连接文本文件?
- 2024-12-09 08:30:00
- admin 原创
- 145
问题描述:
我有 20 个文件名的列表,例如['file1.txt', 'file2.txt', ...]
。我想编写一个 Python 脚本将这些文件连接成一个新文件。我可以通过 打开每个文件f = open(...)
,通过调用 逐行读取f.readline()
,然后将每一行写入该新文件。对我来说,这似乎不太“优雅”,尤其是我必须逐行读取/写入的部分。
在 Python 中有没有更“优雅”的方法来做到这一点?
解决方案 1:
这应该可以
对于大文件:
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 无论如何都应该处理这些文件描述符。我只是觉得这很有趣
解决方案 2:
使用shutil.copyfileobj
。
它会自动为您逐块读取输入文件,这样可以提高效率,读取输入文件时即使某些输入文件太大而无法放入内存,它也能正常工作:
import shutil
with open('output_file.txt','wb') as wfd:
for f in ['seg1.txt','seg2.txt','seg3.txt']:
with open(f,'rb') as fd:
shutil.copyfileobj(fd, wfd)
解决方案 3:
这正是fileinput 的用途:
import fileinput
with open(outfilename, 'w') as fout, fileinput.input(filenames) as fin:
for line in fin:
fout.write(line)
对于此用例,它实际上并不比手动迭代文件简单得多,但在其他情况下,拥有一个迭代器可以迭代所有文件,就好像它们是单个文件一样非常方便。(此外,fileinput
一旦完成就会关闭每个文件,这意味着不需要关闭with
每个close
文件,但这只是一行节省,没什么大不了的。)
其中还有一些其他巧妙的功能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()
解决方案 4:
outfile.write(infile.read()) # time: 2.1085190773010254s
shutil.copyfileobj(fd, wfd, 1024*1024*10) # time: 0.60599684715271s
一个简单的基准测试表明,shutil 的性能更佳。
解决方案 5:
UNIX 命令有什么问题?(假设您不是在 Windows 上工作):
ls | xargs cat | tee output.txt
完成这项工作(如果你愿意的话,你可以用 python 的子进程调用它)
解决方案 6:
我不太清楚优雅与否,但是这样做有效:
import glob
import os
for f in glob.glob("file*.txt"):
os.system("cat "+f+" >> OutFile.txt")
解决方案 7:
如果目录中有很多文件,那么glob2
生成文件名列表可能比手动写入更好。
import glob2
filenames = glob2.glob('*.txt') # list of all .txt files in the directory
with open('outfile.txt', 'w') as f:
for file in filenames:
with open(file) as infile:
f.write(infile.read()+'
')
解决方案 8:
@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 oolder")
stdout, stderr = p.communicate()
这个想法是创建一个批处理文件并执行它,利用“旧的好技术”。它是半 Python 的,但运行速度更快。适用于 Windows。
解决方案 9:
检查 File 对象的 .read() 方法:
http://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects
你可以做类似的事情:
concat = ""
for file in files:
concat += open(file).read()
或者更“优雅”的 Python 方式:
concat = ''.join([open(f).read() for f in files])
根据这篇文章:http: //www.skymind.com/~ocrow/python_string/也是最快的。
解决方案 10:
如果文件不是很大:
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('
') if you want to introduce
# some blank lines between the contents of the copied files
如果文件太大而无法完全读取并保存在 RAM 中,则算法必须略有不同,以固定长度的块循环读取要复制的每个文件,read(10000)
例如使用。
解决方案 11:
def concatFiles():
path = 'input/'
files = os.listdir(path)
for idx, infile in enumerate(files):
print ("File #" + str(idx) + " " + infile)
concat = ''.join([open(path + f).read() for f in files])
with open("output_concatFile.txt", "w") as fo:
fo.write(path + concat)
if __name__ == "__main__":
concatFiles()
解决方案 12:
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)