How To Read And Write Files In Python

Click to share! ⬇️

python files

Python programs can read the information from files, as well as write data to files. While plain old variables are a good way to store data while a program is running, if you want to access that data after the program has finished, you need to save it to a file. Reading from files allows you to work with a wide range of information and writing to files makes it possible for users to pick up where they left off the next time they run the program. You can read data from files, write text to files, and even store Python structures such as lists in data files.

Reading from a file

To read from a file your program needs to open the file and then read the contents of the file. You can read the entire contents of the file at once, or read the file line by line. The with statement makes sure the file is closed properly when the program has finished accessing the file.

Reading an entire file at once

To start, we have a text file in the directory of our Python project so that we can work with it. It’s just a simple file, created using notepad that has some basic text.
python text file

filename = 'pythonfile.txt'
with open(filename) as file_object:
    contents =

python file read output

What is that with a keyword? The with keyword is a context manager. It’s like a shortcut for working with files since it takes care of opening and closing the file automatically for you. For example, if you need to use a file, you would need to open the file and remember to close it once you’re done working with it. The with open statement takes care of this for you.

Inside of the with context, we use the open() function to open the file and return the corresponding file object. If the file is unable to be opened, an OSError is thrown. Using the file object that is now available to us, we can use the read() method to read the file. This method returns the specified number of bytes from the file and the default is -1 which means to read the whole file.

Showing the attributes and properties of the file

filename = 'pythonfile.txt'
with open(filename) as file_object:
    contents =

print('The filename is: ' +
print('The mode is: ' + file_object.mode)

attributes and properties of the file

Reading a file line by line

The other option to read a file is to do it line by line. Here is how to do that.

filename = 'pythonfile.txt'
with open(filename) as file_object:
    for line in file_object:

python loop over file

This time around, we see a lot of extra whitespaces. Why is that? The reason for this is that each line that’s read from the file has a newline character at the end of the line. Using the print() function adds its own newline character. To get around this, we can use the rstrip() method which removes extra blank lines when printing to the screen.

filename = 'pythonfile.txt'
with open(filename) as file_object:
    for line in file_object:

python rstrip example

Modifying output during iteration

As you iterate over a file to output line by line, you can take action on each iteration if you like. Here we have a text file with the following contents.
python replace method

This code will iterate through each line of the file and replace ‘Spanish’ with ‘Mexican’ if found.

file_object = open('replace.txt', 'r')
print('File Contentsn' +

print('nRevised Output')
for line in file_object:
    revised = line.replace('Spanish', 'Mexican')

python replace method on iteration

The above code makes use of a new method we haven’t seen yet, the seek() method. The seek() method uses a pointer to keep track of where we are in the file. Both writing and reading move the seek pointer, and if we want to read the file after writing or reading the file several times, we must set the seek pointer back to zero. We can do this by either running the seek() method or by closing and reopening the file in our code.

Storing the lines in a list

In this snippet of code, we can see how to read a file into a list variable. By printing it out, we see that the contents of the file are indeed a list. We also see how to loop over the list to print out the contents of the file.

filename = 'pythonfile.txt'
with open(filename) as file_object:
    lines = file_object.readlines()
for line in lines:

python read file into list

Writing to a file

You can pass the 'w' argument to the open() function to tell Python you want to write to a file. This will erase anything in the file if it already exists, so be careful not to overwrite any data you wanted to keep.

Creating and writing to a file

filename = 'anotherfile.txt'
with open(filename, 'w') as file_object:
    file_object.write('Writing data to a file!')

python w open example

Write multiple lines to a file

filename = 'anotherfile.txt'
with open(filename, 'w') as file_object:
    file_object.write('This is line one!n')
    file_object.write('This is line two.n')

python multi line file write

Appending to a file

Passing the 'a' argument tells Python you want to append to the end of an existing file. Notice that this time around, we still have the data in the file from the last example. This is the append behavior in action.

filename = 'anotherfile.txt'
with open(filename, 'a') as file_object:
    file_object.write('Appending this line to the file.n')
    file_object.write('Appending another line to the file.n')

python append file example

File paths

The open() function looks for the file in the same directory of the program that’s being executed is stored. To open a file from a subfolder, you can use a relative path. You can also use an absolute path if you like.

Open a file from a subfolder

file_path = 'subfolder/valuabledata.txt'
with open(file_path) as file_object:
    lines = file_object.readlines()
for line in lines:

python Open a file from a subfolder

Opening a file with an absolute path

file_path = 'C:pythonpythonforbeginnerssubfolder\valuabledata.txt'
with open(file_path) as file_object:
    lines = file_object.readlines()

python Opening a file with an absolute path

Storing data with JSON

Python has a JSON module that allows you to dump Python data structures into a file, then load that data from the file the next time the program runs. JSON is a popular data format used in almost all modern programming languages, so you can share this kind of data with others that might not be using Python. It’s important to make sure the data you’re trying to load exists before working with it and if it isn’t, you should use exception handling to manage that.

Using json.dump() to store data

import json
numbers = [3, 6, 9, 12, 15, 18]
filename = 'numbers.json'
with open(filename, 'w') as file_object:
    json.dump(numbers, file_object)

python write json to file

Using json.load() to read data

import json
filename = 'numbers.json'
with open(filename) as file_object:
    numbers = json.load(file_object)

python read json from file

Using FileNotFoundError to manage data

import json
file_name = 'nonexistent.json'
    with open(file_name) as file_object:
        numbers = json.load(file_object)
except FileNotFoundError:
    message = f'Unable to find file: {file_name}.'

python FileNotFoundError example

Reading and Writing files without the with keyword

The examples so far show reading, writing, and appending data while making use of the with keyword. Below are the ways to do these file operations without use with.

Open a file for writing and create it if it doesn’t exist

file_object = open('pythonfile.txt', 'w+')

Open the file for appending text to the end

file_object = open('pythonfile.txt', 'a+')

Write some lines of data to the file

for i in range(10):
    file_object.write('This is line %drn' % (i + 1))

Close the file when done


Open the file back up and read the contents

file_object = open('pythonfile.txt', 'r')

Check to make sure that the file was opened

use the read() function to read the entire file

if file_object.mode == 'r':
    contents =
    print (contents)

readlines() reads the individual lines into a list

if file_object.mode == 'r':
    file_lines = file_object.readlines()
    for line in file_lines:

Using Temporary Files With Python

To use a temporary file in your Python program, you can import the temp_file module. The code below creates a temporary file and stores it in the temp_file variable. Then, we can store some data in the temporary file. In this case, we are saving the date of July 4th, 2020 so that we don’t forget to watch some fireworks. Note that we had to use that lowercase b. This is to turn the string into a bytes object since that is what the write() method is expecting when using the tempfile module. Now recall that when reading or writing to a file, the seek pointer is moved. That is why we set it back to 0 before we try to read the temporary file.

import tempfile

# Create a temporary file
temp_file = tempfile.TemporaryFile()

# Write to a temporary file
temp_file.write(b'Save the date: 070420')

# Read the temporary file

# Close the temporary file

Working With The ZipFile Module

It’s easy to create and work with file archives in Python. Let’s see a few examples of how this works. We’ll work with a couple of text files that we have in our directory, ships.txt, and characters.txt.
text file to archive with python
second text file to archive with python

Create a file

# Zipfile Module
import zipfile

# create a ZipFile object
zip_object = zipfile.ZipFile('', 'w')

Add two file to the archive and close it

# Add two files to the zip_object

# close the Zip File

List the contents of the archive

# Open and List contents
zip_object = zipfile.ZipFile('', 'r')
['ships.txt', 'characters.txt']

Get metadata of files in the archive

# Metadata in the zip archive
for meta in zip_object.infolist():
<ZipInfo filename='ships.txt' filemode='-rw-rw-rw-' file_size=84>
<ZipInfo filename='characters.txt' filemode='-rw-rw-rw-' file_size=59>
#Metadata of a single file in the archive
info = zip_object.getinfo('ships.txt')
<ZipInfo filename='ships.txt' filemode='-rw-rw-rw-' file_size=84>

Read files in the archive

# Read a file in the zip archive
Millennium Falcon
The Ghost
Jedi Starfighter
The Fireball
with'characters.txt') as file_object:
Princess Peach

Extract single or multiple files from the archive


Learn More About Working With Files In Python

How To Read And Write Files In Python Summary

Python provides built-in methods for working with files. You can open files, write data to them, read the data back in, etc. All the things you expect to do with files can be done in Python. You don’t need to import a library in order to read and write files. Using Python’s built-in open function to get a file object is the first step in working with files. This returns a file object which we saw in several of the examples in this tutorial. There are several methods and attributes of file objects that can be used to learn about the file you opened. They can also be used to manipulate the file. So in this tutorial, we looked at reading from a file, showing the attributes and properties of the file, reading a file line by line, storing lines of a file in a list, Writing to a file, Write multiple lines to a file, Storing data with JSON, creating temporary files, and working with the ZipFile Module to create and read file archives.

Click to share! ⬇️