0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

 【備忘】Pythonのコンテキストマネージャーネストをワンライナーで書く方法

Posted at

はじめに

Pythonでコンテキストマネージャーのネストをワンライナーにできることを知ったので備忘として残しておきます。

ネストされた通常の書き方

with open('input.txt', 'r') as input_file:
    with open('output.txt', 'w') as output_file:
        print("何かの処理")

ワンライナーでの書き方

with open('input.txt', 'r') as input_file, open('output.txt', 'w') as output_file, open('log.txt', 'a') as log_file:
    print("何かの処理")

注意点

  • Python3.1以降でのみ利用可能
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?