LoginSignup
2
4

More than 5 years have passed since last update.

Pythonでファイルを一行ずつ読み込む

Posted at

Pythonでファイルから一行ずつ読み込み - Qiita

ファイルを読むにはwith文を使うのが良いようだ。

def main():
    import inspect
    filename = inspect.currentframe().f_code.co_filename

    with open(filename) as fh:
        for line in fh:
            print(line.strip())


if __name__ == '__main__':
    main()
    exit(0)
2
4
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
2
4