oldoc63 / learningDS

Learning DS with Codecademy and Books
0 stars 0 forks source link

Iterating Through Lines #354

Open oldoc63 opened 2 years ago

oldoc63 commented 2 years ago

When we read a file, we might want to grab the whole document in a single string, like .read() would return. But what if we wanted to store each line in a variable? We can use the readlines() function to read a text file line by line instead of having the whole thing.

oldoc63 commented 2 years ago

The above script creates a temporary file object called keats_sonnet that points to the file keats_sonnet.txt. It then iterates over each line in the document and prints the entire file out.