Python Open File – How to Read a Text File Line by Line

PHOTO EMBED

Mon Mar 14 2022 04:38:47 GMT+0000 (Coordinated Universal Time)

Saved by @ghefley #python

file = open("demo.txt")
print(file.read())
content_copyCOPY

The read() method is going to read all of the content of the file as one string. This is a good method to use if you don't have a lot of content in the text file. In this example, I am using the read() method to print out a list of names from the demo.txt file: file = open("demo.txt") print(file.read())

https://www.freecodecamp.org/news/python-open-file-how-to-read-a-text-file-line-by-line/