How do I print the content of a .txt file in Python?

前端 未结 9 988
慢半拍i
慢半拍i 2020-12-13 18:28

I\'m very new to programming (obviously) and really advanced computer stuff in general. I\'ve only have basic computer knowledge, so I decided I wanted to learn more. Thus I

9条回答
  •  没有蜡笔的小新
    2020-12-13 19:02

    Opening a file in python for reading is easy:

    f = open('example.txt', 'r')
    

    To get everything in the file, just use read()

    file_contents = f.read()
    

    And to print the contents, just do:

    print (file_contents)
    

    Don't forget to close the file when you're done.

    f.close()
    

提交回复
热议问题