How do I Open a text file in python?
# text.txt: # Hello, # this is a beautiful document. # Goodbye. with open('test.txt', 'r') as file: print(file) for line in file: print(line.rstrip())
<_io.TextIOWrapper name='test.txt' mode='r' encoding='UTF-8'> Hello, this is a beautiful document. Goodbye.
When looping through the lines of the text file, the newline character at the end of each line is included. In this example, it is stripped with rstrip()
Link to documentation or other source
10 Jun 2019
Author: Loek van den Ouweland
Would you recommend this example to your colleagues?