Python Read and Write Files

Sometimes you need to work with external files in Python. Perhaps you want to read a configuration file from a network device or you need to parse a log file. Fortunately, Python can read and write files.

Open() Function

To work with files in Python, we need to use the open() function. There are three options to work with files:

  • Read (r): This is the default option which opens a file to read.
  • Write (w): Open a file and write to it. Overwrites any current content in the file.
  • Append (a): Opens the file and writes to it but instead of overwriting, appends to the file.

Python can work with text or binary (JPG, PNG, MP3, etc.) files.

Let’s see what we can do.

Open File

I’ll create a text file named “demo.txt” with the following contents:

In our code, we didn’t specify whether we wanted to read, write, or append to the file. We also didn’t specify whether we wanted to open it in text or binary mode. We don’t have to because the default options are “r” (read) and “t” (text). If you do want to specify our options, it looks like this:

So far, so good. We have the contents of our file. However, Python returned it as a string. Wouldn’t it be easier if each line was a separate string? We can do this with the readlines() function:

Run the code above, and it shows each line as a string within a list.

Close File

In the example above, we opened a file but we never closed it. Closing files is important, especially when you write files.  Some changes in files won’t show up until you close them. In Microsoft Windows, open files are locked which prevents other programs from opening or writing to your files. Here’s an example of how to close your file:

First, we open the file, then we do something with it, and finally, we close the file.

With Open

Instead of using the separate open() and close() function, we can also use the with block. You put everything you want to do with the file under the with block, and afterward, Python automatically closes the file. Here is an example:

I personally prefer this way. The indentation makes it easier to recognize what you do with the file in your code and you don’t have to worry about not closing the file.

Write File

When you write to a file, you overwrite its contents. When the file doesn’t exist, Python creates the file. To write to the file, we need to add the “w” parameter to the open() function. Here is our code:

The code above writes a string to the “demo2.txt” file. We could use a text editor to check the contents of the file but since we are using Python, we’ll open it in Python as well.

If you run this code multiple times, it always shows a single line. That’s because the write parameter overwrites the content of our file.

Append File

Instead of overwriting the content of our file, we can append to the file. To do this, you have to use the “a” parameter in the open() function:

When we run the above code multiple times, you’ll see that Python appends the string to our file. There is one problem though. Python appends the string to the same line.

New Line

If you want to append to a new line, you have to use the \n new line symbol in your string. Here’s our code:

In the code above, I added the \n new line symbol before my string. Each line we append is now saved in a new line of our file.

Delete File

What if we want to delete a file? We can do this with the OS module and the os.remove() function. This doesn’t work on the embedded Python interpreter so you might want to try this on your own computer. Here is the code:

When you run the code above, it won’t show you any output. It just deletes the file. If the file doesn’t exist, it will throw an error:

This is a good example of why you should use an if statement. Here is the code:

In the code above, we check whether the file exists. If so, we delete it. Otherwise, we print a message.

Conclusion

You have learned how to read and write files in Python:

  • To work with files in Python we use the open() function.
  • Python supports text and binary files.
  • The default modes are “read” and “text”.
  • When you open or write to files, you should always close them.
  • Using the with block is a good idea because it automatically closes your file.
  • You can write (overwrite) or append to a file.
  • You can delete files with the OS module.

I hope you enjoyed this lesson. Please leave a comment if you have any questions.


Ask a question or start a discussion by visiting our Community Forum