Pythonのwith文で複数のファイルを開く
「input.txt」の内容を「output.txt」に書き込むプログラム
with (open('input.txt') as file_input, open('output.txt', mode='w') as file_output): for row in file_input: file_output.write(row)
withの後に()を書いて、その中に複数のopenを書く方法はPython3.9から使えるようになったそうです。
www.python.jp
以前はバックスラッシュ「\」を使って、下記のように書く必要があったそうです。
with open('input.txt') as file_input, \ open('output.txt', mode='w') as file_output: for row in file_input: file_output.write(row)