Learn Python - File Input / Output - The 'with' and 'as' Keywords

This community-built FAQ covers the “The ‘with’ and ‘as’ Keywords” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise The ‘with’ and ‘as’ Keywords:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

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

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

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

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

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

Not seeing your question? It may still have been asked before – try searching for it by clicking the spyglass icon (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

Hi, so the below code succeeds in writing to the file and then printing it off in read mode:

with open("text.txt", "w") as textfile:
  textfile.write("Success!")

with open("text.txt", "r") as readfile:
  print readfile.read()

however this doesn’t print anything out:

with open("text.txt", "r+") as textfile:
  textfile.write("Success!")
  print textfile.read()

I thought that with the “r+” being the read and write mode, I’d be able to write to the file and print out the read() command under the same bit of code? Is this not possible or is my syntax wrong or…? Thanks in advance

Ok, so let us assume that this is the content of the text.txt file:

Lorem ipsum dolor sit amet...

Single line of text, nothing fancy.

Execution of this code:

with open("text.txt", "r+") as textfile:
  textfile.write("Success!")
  print textfile.read()

will result in:

  1. content of the file being changed to:
    Success!sum dolor sit amet...
    
  2. and printing out in the console text:
    sum dolor sit amet...
    

Do you see what happened? When you open the file with the r+ mode the pointer / cursor is being placed at the beginning of the file. Then you write the text (overwriting part of the existing content) and now the pointer is placed after the exclamation mark.
And when you use read it will start from the point where the pointer is now. That’s why you get in the result “sum dolor sit amet…”.


So I assume that your text.txt file is empty and that is why you don’t see any output.

1 Like

Ah wow that’s interesting - thanks for the clear explanation!
Is there a way to reset this pointer whilst staying in r+ mode?