Introduction
As pointed out in a previous article that deals with reading data from files, file handling is essential knowledge of every professional and hobbyist Python programmer. This feature is a core part of the Python language, and no extra module needs to be loaded to do it properly.
In this article, we will take a look at how to write data to a file line by line, as a list of lines, and append data at the end of a file.
Basics of Writing Files in Python
There are three common functions to operate with files in Python:
open()
to open a file,seek()
to set the file's current position at the given offset,close()
to close the file afterwards.
Note: open()
is a built-in Python function that returns a file handle that represents a file object to be used to access the file for reading, writing, or appending.
Writing to a file requires a few decisions - the name of the file in which to store data and the access mode of the file. Available are two modes, writing to a new file (and overwriting any existing data) and appending data at the end of a file that already exists. The according abbreviations are "w", and "a", and have to be specified before opening a file.
Note: Besides mentioned "w" and "a" flags, there is another, more powerful one - the "w+" flag, which opens file for both reading and writing.
Writing a Single Line to a File
This first example is pretty similar to writing to files with the popular programming languages C and C++. The process is pretty straightforward. First, we open the file using the built-in open()
function for writing, write a single line of text to the file using the write()
method, and then close the file using the close()
method. Keep in mind that due to the way we opened the helloworld.txt
file it will either be created if it does not exist yet, or it will be completely overwritten:
filehandle = open('helloworld.txt', 'w')
filehandle.write('Hello, world!\n')
filehandle.close()
This entire process can be shortened using the with
statement:
with open('helloworld.txt', 'w') as filehandle:
filehandle.write('Hello, world!\n')
As already said before, keep in mind that opening the helloworld.txt
file this way will either create if it does not exist yet or completely overwrite it.
Writing a List of Lines to a File
In reality, a file does not consist only of a single line, but much more data. Therefore, the contents of the file are stored in a list that represents a file buffer. To write the entire file buffer we'll use the writelines()
method:
filehandle = open("helloworld.txt", "w")
filebuffer = ["a first line of text", "a second line of text", "a third line"]
filehandle.writelines(filebuffer)
filehandle.close()
Running the previous Python program and then using the cat
command we can see that the file helloworld.txt
has the following content:
$ cat helloworld.txt
a first line of texta second line of texta third line
This happens because the writelines()
method does not automatically add any line separators when writing the data, even though intuitively, it should write individual lines.
This can easily be remedied by adding a \n
(newline) character at the end of each line. Additionally, we can simplify the loop using a generator expression:
with open('helloworld.txt', 'w') as filehandle:
filebuffer = ["a line of text", "another line of text", "a third line"]
filehandle.writelines(f"{line for line in filebuffer}\n")
Now, the output file helloworld.txt
has the desired content:
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
$ cat helloworld.txt
a first line of text
a second line of text
a third line
Appending Data to a File
So far, we have stored data in new files or in overwritten data in existing files. But what if we want to append data to the end of an existing file? In this case, we would need to open the existing file using a different access mode. We change the access code to 'a'
instead of 'w'
:
filehandle = open('helloworld.txt','a')
filehandle.write('\n' + 'Hello, world!\n')
filehandle.close()
As before, we can also rewrite the previous code using the with
statement:
with open('helloworld.txt', 'a') as filehandle:
filehandle.write(f'\n Hello, world!\n')
Using the same helloworld.txt
file from before, running this code will produce the following file contents:
$ cat helloworld.txt
Hello, world
a second line
and a third line
Hello, world!
Conclusion
Writing plain text data to files, or appending data to existing files, is as easy as reading from files in Python. As soon as a file is closed after writing or appending data, Python triggers a synchronization call. As a result, the updated file is immediately written to disk.