私はshutil.copyfileobjを使用したいと思います。これをglob.globと簡単に組み合わせて、一連のファイルをパターンごとに連結できます。
>>> import shutil
>>> infiles = ["test1.txt", "test2.txt"]
>>> with open("test.out","wb") as fout:
for fname in infiles:
with open(fname, "rb") as fin:
shutil.copyfileobj(fin, fout)
glob.globと組み合わせる
>>> import glob
>>> with open("test.out","wb") as fout:
for fname in glob.glob("test*.txt"):
with open(fname, "rb") as fin:
shutil.copyfileobj(fin, fout)
しかし、それ以上に、posixユーティリティを使用できるシステムを使用している場合は、その使用をお勧めします
D:\temp>cat test1.txt test2.txt > test.out
Windowsを使用している場合は、コマンドプロンプトから次のコマンドを発行できます。
D:\temp>copy/Y test1.txt+test2.txt test.out
test1.txt
test2.txt
1 file(s) copied.
注
最新の更新に基づく
はい、同じ行数で、1つのファイルのすべての行を他のファイルと結合したいと思います
with open("test.out","wb") as fout:
fout.writelines('\n'.join(''.join(map(str.strip, e))
for e in zip(*(open(fname) for fname in infiles))))
そして、posixシステムでは、次のことができます
paste test1.txt test2.txt