How to Write a File in Python

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Video Tutorial

Writing a file

Overview

Python provides us with two methods to write into text files:

  • write() method for inserting a string to a single line in a text file.
  • writelines() method for inserting multiple strings from a list of strings to the text file simultaneously.

Scope

  • Moving ahead in this Python series, we will learn how to write into text and binary files in Python
  • Firstly, we will see how to write into text files using different methods present in Python, along with suitable examples. Then, we will learn how to write into binary files in Python along with an example.

Writing to a text file in Python

We have various access modes to open an existing text file in Pyhton and to write to it, depending on what we need. Even if the text file does not already exist, we can use the w or the a access mode to create a text file and then write to it. There are two functions in Python that help us write into text files:

1. The write() function in Python:

Whatever strings you give as a parameter to this function, it writes it as a single line in the text file. Now again, whether or not the existing contents will be truncated depends on the access mode. If you use the w mode, the contents will be truncated, and your string will be written. However, in the a mode, your existing contents will not be deleted, and your string will be written after the contents.

Now let's take a look at an example. We initially do not have any existing file with the name of writing.txt. We're creating it using the w mode. Once we open the new file, it's obviously empty. We're then going to write content into it.

Here, file_obj is the file object. We're opening a file named writing.txt in the w access mode. If this file does not exist, since we have used the w access mode, it will be created.

We can now write into it like this:

Since we used the open() method to open the file, instead of with open(), we need to close the file as well.

Let’s see what our file looks like now with the below program.

The non-existent file now has above two lines of content in it. Let's try writing to the file again, just another line. Since we closed the file earlier, we need to open it again.

Note how we used the w access mode. The file writing.txt currently exists. The previous time we used the open function to open the file, it did not exist. We created it and then opened it. This time, it just opens in writing mode since it already exists.

Once you run this code, you see the below file contents:

To keep the initial contents of the file, you will have to use the append (a) access mode. Since you will change the access mode, you need to re-open the file in that mode after closing it.

Output

2. The writelines() function in Python:

Constantly using file.write() for every line that we want to write to your file can get difficult. Hence, we can use the writelines() function.

A simple way to use it, is to provide a list of strings as a parameter to writelines().

Note that if you open a file with w or a access mode, you can only write to the file and not read from it. In the same way, if you open a file in the r mode, you can only read from it and not write. If you wish to perform both operations simultaneously, you should use the a+ mode.

Writing to a binary file in Python

After learning how to read data from a binary file, I'm sure you know how to write to it too. We use the wb mode to write to a binary file.

Example

Obviously, binary data is not human recognizable. So when we have to write an array of numbers like 1, 2, 3, 4, and 5, we need to first convert them into a byte representation to store it in the binary file. For that purpose, we use the built-in bytearray() function.

You now know all you need to know about writing into files using Python.

Conclusion

After learning about how to write into files in Python, here are some key points that will help you:

  • We use the write() method to insert a single string into a text file and the writelines() method to insert multiple strings into a text file.
  • To write something to a binary file, we need to first convert our input into binary form.
  • To convert an array of numbers into a binary array, we can use bytearray() function.

Read More: