Go to: Na-Rae Han's home page  

Python 2.7 Tutorial

With Videos by mybringback.com

#16: Writing Text to a File

                          << Previous Tutorial           Next Tutorial >>
On this page: writing to a file, open('...', 'w'), .writelines(), .close().

Get Started

Video Summary

  • Interactive IDLE session from this tutorial.
  • Writing a file within Python involves defining a variable that Python will use as shorthand for your new file, then connecting it to the open() function. From there, you can tell the open() function to open a file that does not yet exist with the addition of a special write command. This sounds more complicated than it actually is. For example, the command outfile = open('C:/Users/mybringback/Desktop/output.txt', 'w') will create a blank .txt file titled "output" on the Desktop. The first string within this open() function is the decided location of your new file (your location can and will differ from the one above), while the 'w' cited as a string within the open() function is the write command mentioned above. Without this command, Python will return an error, since Python will think you are simply trying to open a file that is not actually there.
  • In order to get content into that new file, you can use the .writelines() function to modify your variable. This involves connecting your variable (in this scenario, "outfile") to the .writelines() function and then passing it an argument which will contain your new content. These arguments can be made up of many different items commonly used within Python, but for its argument this tutorial uses the previously defined list "myText" (for more information on lists and how to define them, see Tutorial 8). As such, the command outfile.writelines(myText) will write the contents of the "myText" list to the variable "outfile", which as mentioned earlier is defined as your new file output.txt.
  • Remember that before Python can complete writing to your new file, you must close the file within Python to finish the job. This can be done by calling the close() method on your file object variable. For example, the command outfile.close() will close the "outfile" object and allow Python to complete its task.
  • You can now open up the output.txt text file to verify that writing was successful. The result is all the words in the original list myText thrown together:

Learn More

  • What if your file "output.txt" already exists and you want to add the content at the end of it? For that, you can use the 'a' switch (for 'append') instead of the 'w' switch.
  • File IO (input/output) is the area where new programmers often have trouble with. Make sure you learn the content of this advanced topic page: "File Path and CWD".
  • File writing has an additional method, plus some pitfalls to watch out for. See "File Reading and Writing Methods" for details.


  • Anne Dawson has many sample scripts for File I/O. Search for "open". Note that she uses the "escaped backslash" style (see this page) of Windows file path reference.