FAQ: Learn Python: Files - Review

This community-built FAQ covers the “Review” 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 Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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!

We learnt how to append files as text, but how is that for CSV and JSON?

1 Like

Yanno…it just takes something little to throw one off. I missread jason_txt as ‘jason,txt’ in problem 12 and it took me a day to realize my error in reading the question. It is no wonder that the invalid syntax lines said no string attribute.

Sometimes it is the littlest and simplest things that throw one off in this course. I don’t know what made me think of j’json.txt’ in the function call jsondump.

So I had this funny mistake during this lesson that I wanted to ask about:

When doing .readline() and .readlines()

Why is it that if you iterate through .readline() the same way you can iterate through readlines(), you get all letters on individual lines?

with open(‘just_the_first.txt’) as txt_doc:
for line in txt_doc.readlines():

So in this example above (from the lesson), it will read each line in the text, and print them out, but if I turn it to .readline() instead it prints individual letters. How does it work? What is happening?

I know this question might have no practical use, and is just to satisfy my curiousity, but I appreciate any response. I just want to understand the inner workings/logic of the language.

Thanks in advance.

Consider what for is… An iterator, of sorts. It iterates over a sequence, an iterable. If it is not a sequence, it cannot be iterated and will throw an exception.

If we give a string sequence to for it iterates the sequence by element, just it would a list, tuple, set, or even dictionary (iterating the keys or the values). .readline() returns a single string, so therefore the iterable.

.readlines() takes one line at a time, so the sequence is the file rows, not an individual line. The difference is subtle, but pronounced enough that we get the gist in pretty short order.

Let’s take for example the list() constructor. It, too, is an interator of sorts, since it iterates over the sequence it is given, and constructs a list object from that sequence.

list('string')  =>  ['s', 't', 'r', 'i', 'n', 'g']

Not the same thing, but an analog, along the same lines.

1 Like

Thanks alot for your response! This really cleared it up for me ^^ When you explain it to me now, I realize this also was explained in the “Strings” module of the course :sweat_smile: . Really appreciate the explaination! :smiley:

1 Like