How to Write or Print to a File in Python

Originally posted on makeuseof.

If you’re getting started with Python, you’ll need to know how to print to a file. Follow this short tutorial to learn how.

Need to print to a file in Python? Today we’ll find out how easy it is to start writing to files. We’ll cover creating new files, appending existing files, and overwriting existing files.

Open a File For Writing in Python

You probably already know how to print on screen in Python, but you might not know how to print to a file. Fortunately, like much beginner Python programming, the syntax of file writing is simple, readable, and easy to understand.

With that in mind, let’s get started.

Create and Write to a New File in Python

To create a new file in Python and open it for editing, use the built-in open() function and specify the file name followed by the x parameter.

f = open("testfile.txt", "x")

When using the “x” parameter, you’ll get an error if the file name you specified exists already.

If it’s successful, you can now write to the file using the write() method.

f.write("Hello, world!")

Each line of text you “write()” will be terminated with an end-of-line character, so each additional string will be written in a new line.

It’s good practice to always close any file you open using the close() method. Otherwise, your file may not get saved to disk.


You can also create and write to a file in Python with fewer lines using the with keyword.

with open("testfile.txt", "x") as f:
f.write("Hello, world!")

This approach is recommended because the “with” suite will close your file automatically after finishing, so you never have to remember to close it yourself.

After writing your file, you can read it by opening with the r parameter and calling the read() method.

with open("testfile.txt", "r") as f:

Write to an Existing File in Python

If the file you want to write to exists already, and you want to add additional lines to it, you’ll need to open it using the a parameter for “append.”

with open("testfile.txt", "a") as f:
f.write("I'm an additional line.")

Anything you write after opening with the “a” parameter will be appended with a new line.

This code also assumes your file is in the same directory your Python script is operating in. If it’s in a different directory, you’ll need to specify its path.

Overwrite an Existing File in Python

If your file already exists, but you want it overwritten instead of appended, you can do that by opening the file with the w parameter.

with open("testfile.txt", "w") as f:
f.write("Hello, world!")

No matter what was written in testfile.txt, the output will be “Hello, world!” when you read it.

Troubleshooting File Writing in Python

If the text you’re printing to file is getting jumbled or misread, make sure you always open the file with the correct encoding.

with open("testfile.txt", "w", encoding="utf8") as f:

Most text files these days use UTF-8 encoding, but some other common ones are ISO-8859 (iso-8859-1), UTF-16 (utf16), or Windows-1252 (cp1252).

Your Python toolbelt now includes the ability to print to a file, a frequent task in scripting.

Source: makeuseof

Interested in a Python Course? Then contact us:

* These fields are required.