Hi so I just started learning how to read files in Python. So, I started writing a file using Python using the code:
with open('dog_breeds.txt', 'w') as writer:
writer.write("Pug\r\nJack Russell Terrier\r\nEnglish Springer Spaniel\r\nGerman Shepherd\r\nStaffordshire Bull Terrier\r\nCavalier King Charles Spaniel\r\nGolden Retriever\r\nWest Highland White Terrier\r\nBoxer\r\nBorder Terrier\r\n")
writer.close()
This results in a .txt file which image can be seen here.
Then, I start to run the following code:
import time
reader = open('dog_breeds.txt', 'r+')
line = reader.readline()
print('Output of readline while loop is: ')
while line != '':
print(line, end='')
time.sleep(1)
line = reader.readline()
reader.close()
Then, I got this output when I execute the code:
Output of readline while loop is:
Pug
Jack Russell Terrier
English Springer Spaniel
German Shepherd
Staffordshire Bull Terrier
Cavalier King Charles Spaniel
Golden Retriever
West Highland White Terrier
Boxer
Border Terrier
So, I am a little confused. I thought that the end = '' argument in the print() will cause each line in the .txt file to be printed at the end of the previous line that is printed, resulting in one long line being displayed (i.e. PugJackRussellTerrierEnglish...). Instead, what is displayed is multiple lines with each line in the .txt file being a separate line. Is there something I am misunderstanding?