FAQ: Learn Python: Files - Writing a File

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

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!

A post was split to a new topic: I found this frustrating

3 posts were split to a new topic: Is there a way to open a file in both read and write?

Write, Read, Tell and Seek

with open("bad_bands.txt", 'r+') as bad_bands_doc:
  print(bad_bands_doc.tell())
  bad_bands_doc.write("Captain Beefheart")
  print(bad_bands_doc.tell())
  bad_bands_doc.seek(0,0)
  print(bad_bands_doc.tell())
  myRead = bad_bands_doc.read()
print(myRead)

1 Like
with open('bad_bands.txt', 'w') as bad_bands_doc:
  bad_bands = bad_bands_doc.write("FT Islands")

print(bad_bands)

This outputs 10 somehow.
I was expecting to see FT Islands.
Why is this returning 10(seems like that is the length of the string FT Islands)?

1 Like

I wrote the following code which was deemed as correct. But when I tried to test it with a print() statement, I got a weird answer, Can anyone advise why “Beatles” did not get printed please?

You print the file object, not the content of the file.

A lot is happening when it comes to reading and writing from files. The print gives us some information about the module (io) and the class (TextIOwrapper).

IO stands for Input and Output.

You could read from the file, and print that. Certainly possible.

1 Like

Thanks for the answer. I tried doing that too- pls see attached the response I got- why I am not able to read the file?

you made on line 4, you misspelled band.

1 Like

Thanks, it worked!! But I still don’t completely understand why it didn’t print earlier when I used the ‘write’ function…will continue to work on that

with open(‘bad_bands.txt’, ‘r+’) as bad_bands_doc:

bad_bands_doc.write(‘any band’)

test = bad_bands_doc.read()

print(test)

I wanted to open a file for read and write. So I wrote something and I wanted to read it. It did not output anything

I covered exactly this in a split of this topic:

This entire chapter is bugged for me. Keeps giving me errors even though my code is EXACTLY what the solution is. When I copy the solution it works sometimes and not others.

Until now all the arguments, functions and etc had almost clear names of what they do and are somewhat easy to remember.

Why all of the suden the arguments for files are just “w”, “r”, “a”…
Are there better arguments for the open() function?