Sometimes you don’t want to iterate through a whole file. For that, there’s a different file method, .readline(), which will only read a single line at a time. If the entire document is read line by line in this way subsequent calls to .readline() will not throw an error but will start returning an empty string (""). Suppose we had this file:


I shall forget you presently, my dear, So make the most of this, your little day, Your little month, your little half a year, Ere I forget, or die, or move away,


with open('millay_sonnet.txt') as sonnet_doc: first_line = sonnet_doc.readline() second_line = sonnet_doc.readline() print(second_line)

This script also creates a file object called sonnet_doc that points to the file millay_sonnet.txt. It then reads in the first line using sonnet_doc.readline() and saves that to the variable first_line. It then saves the second line (So make the most of this, your little day,) into the variable second_line and then prints it out.



Using a with statement, create a file object pointing to the file just_the_first.txt. Store that file object in the variable first_line_doc.


Save the first line of just_the_first.txt into the variable first_line.


Print out the variable first_line.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?