Home » Tutorials » Python » Writing to Files

Writing to Files

Reading files is cool and all, but writing to files is a whole lot more fun. It also should instill a sense of danger in you because you can overwrite content and lose everything in just a moment. Despite the threat of danger, we press on. Python makes writing to files very simple. With somewhat similar methods to reading, writing has primarily 2 methods for writing. Let’s get to it!

Warning! The Evil “w” in the Open Method

Example
f = open("test.txt","w") #opens file with name of "test.txt"
f.close()

…And whoops! There goes our content. As I said, we are in dangerous water here friends. As soon as you place “w” as the second argument, you are basically telling Python to nuke the current file. Now that we have nuked our file, let’s try rebuilding it.

Example
f = open("test.txt","w") #opens file with name of "test.txt"
f.write("I am a test file.")
f.write("Maybe someday, he will promote me to a real file.")
f.write("Man, I long to be a real file")
f.write("and hang out with all my new real file friends.")
f.close()

If you were continuing from the last tutorial, we just rewrote the contents that we deleted to the file. However, you might be flipping out screaming, “but it’s not the same!” You are 100% correct my friend. Hit the control key a couple of times and cool off, I’ll show you the fix in a minute. Ultimately, the write() method is really easy. You just pass a string into it (or a string variable) and it will write it to the file following that one way process it does. We also noticed that it kept writing without using any line breaks. Let’s use another method to fix this.

Writing Lines to a File

We have the a fairly easy solution of just putting a new line character “\n” at the end of each string like this:

Example
f.write("Maybe someday, he will promote me to a real file.\n")

It’s just a simple text formatting character. Yes, even text files have a special formatting similar to how HTML documents have their own special formatting. Text files are just much more limited than HTML.

Appending to a File

Example
f = open("test.txt","a") #opens file with name of "test.txt"
f.write("and can I get some pickles on that")
f.close()

Boom! While our text file makes absolutely no sense to a human, we both know we just had a big victory. The only big change here is in the open() method. We now have an “a” (for append) instead of the “w”. Appending is really just that simple. Now go out there and write all over the world my friend.



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "Writing to Files". After Hours Programming. Accessed on September 20, 2024. https://www.afterhoursprogramming.com/tutorial/python/writing-to-files/.

  • Stewart, Suzy. "Writing to Files". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/python/writing-to-files/. Accessed 20 September, 2024.

  • Stewart, Suzy. Writing to Files. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/python/writing-to-files/.



8 thoughts on “Writing to Files”

  1. i get this “todo; haven’t implemented non-read opens” when i implement in the code simulator.

    the code im running:

    f = open(“output.txt”, “a”)
    f.write(“hi there
    “)
    f.close()

  2. Barak just open it with append and change the line to ” ”
    like so:
    f = open(“test.txt”,”a”)
    f.write(“”)[the line number you want]
    f.close()
    #enjoy 🙂
    ~Mr Guru

    • Is it possible to write variables to a txt file? For example, if I had name = input(“Enter your name: “)
      and the user entered their name, would I be able to write that to a txt file? And if so, how?

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.