FAQ: Learn Python: Files - Reading a Line

This community-built FAQ covers the “Reading a Line” exercise from the lesson “Learn Python: Files”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Python 3

FAQs on the exercise Reading a Line

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

11 posts were split to a new topic: I accidently used .readlines(), but why does it look different than in the last exercise?

Read file line by line in python:

with open("sample.txt") as f:
  for line in f:
    print(line)

What code needs to be in the “with…as… :” block of code? For example, does “print(first_line)” need to be in this block? How do I know when I can leave this code block?

Any operations related to the file should happen in this block. Once you are done with the file operations, you can leave the block.

1 Like

So, I’m stuck on the very first step. It says:

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 .

I typed:

with open ('just_the_first.txt') as first_line_doc:

And it’s not letting me advance to the next step. I’ve been staring at this for about ten minutes, and finally just skipped ahead and did the other greyed-out steps, and successfully printed out the first line, but it’s still not letting me advance.

The little black popup keeps asking, by the way:

Did you open just_the_first.txt as first_line_doc ?

So, what stupidly obvious thing am I missing here?

SCT very likely does not approve of the (white)space between open and (

1 Like

Holy moley, that was it.

How does python know to read the second line when you run readline() for a second time? This is the first piece of code I have encountered that acts differently based on how many times it has been called previously in a script. For instance, I would expect that

line_one=text_doc.readline()
line_two=text_doc.readline()

would result in two identical variables. But you get the first line and second line of the document text_doc.

1 Like

here is a simple example of how something could be achieved:

class MyFile:
    line_number = 0

    def __init__(self, file):
        self.file = file
    
    def read_line(self):
        line = self.file[self.line_number]
        self.line_number += 1
        return line

file = MyFile([
        'hello',
        'world'
    ])
print(file.read_line())
print(file.read_line())

but I am sure real implementation is different and better and using buffer.

3 Likes