こんにちは簡単な質問です。私は完璧に動作するファイルの連結を持っているが、それはちょっと混乱している。これを記述するだけで、よりエレガントな方法があった場合、私は疑問に思っ:ファイルを組み合わせるエレガントな方法 - Python
path = path/to/file/location
with open(path + 'result.txt', 'w') as result, \
open(path + 'file1.txt') as f1, \
open(path + 'file2.txt') as f2, \
open(path + 'file3.txt') as f3, \
open(path + 'file4.txt') as f4, \
open(path + 'file5.txt') as f5, \
open(path + 'file6.txt') as f6, \
open(path + 'file7.txt') as f7, \
open(path + 'file8.txt') as f8, \
open(path + 'file9.txt') as f9, \
open(path + 'file10.txt') as f10, \
open(path + 'file11.txt') as f11, \
open(path + 'file12.txt') as f12, \
open(path + 'file13.txt') as f13, \
open(path + 'file14.txt') as f14, \
open(path + 'file15.txt') as f15, \
open(path + 'file16.txt') as f16:
for line1, line2, line3, line4, line5, line6, line7, line8, \
line9, line10, line11, line12, line13, line14, line15, line16 \
in zip(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16):
result.write('{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, \
{}, {}, {}\n'.format(line1.rstrip(), line2.rstrip(), line3.rstrip(), line4.rstrip(), \
line5.rstrip(), line6.rstrip(), line7.rstrip(), line8.rstrip(), line9.rstrip(), \
line10.rstrip(), line11.rstrip(), line12.rstrip(), line13.rstrip(), line14.rstrip(), \
line15.rstrip(), line16.rstrip()))
おかげ
'with 'で可能かどうかはわかりませんが、ファイル名をリストに入れて(または単に' range'を使って)、ファイルをループで手動で 'open'および' .close'することができます。 –
[この質問](http://stackoverflow.com/a/3025119/2399799)を参照してください。 –
ああ、残念ですが、関数の一部ですので、スクリプト内で 'with'が動作します。これはちょうど面白いビットです。 – ben