Python provides a wide range of built-in functions for file handling. It makes it really easy to create, update, read, and delete files.
Open a File
When you want to work with a file, the first thing to do is to open it. You can open a file using open() built-in function specifying its name.
f = open('myfile.txt')
When you specify the filename only, it is assumed that the file is located in the same folder as Python. If it is somewhere else, you can specify the exact path where the file is located.
f = open(r'C:\Python33\Scripts\myfile.txt')
Remember! While specifying the exact path, characters prefaced by \ (like \n \r \t etc.) are interpreted as special characters. You can escape them using:
- raw strings like
r'C:\new\text.txt'
- double backslashes like
'C:\\new\\text.txt'
Specify File Mode
Here are five different modes you can use to open the file:
Character | Mode | Description |
‘r’ | Read (default) | Open a file for read only |
‘w’ | Write | Open a file for write only (overwrite) |
‘a’ | Append | Open a file for write only (append) |
‘r+’ | Read+Write | open a file for both reading and writing |
‘x’ | Create | Create a new file |
You can also specify how the file should be handled.
Character | Mode | Description |
‘t’ | Text (default) | Read and write strings from and to the file. |
‘b’ | Binary | Read and write bytes objects from and to the file.This mode is used for all files that don’t contain text (e.g. images). |
Here are some examples:
# Open a file for reading
f = open('myfile.txt')
# Open a file for writing
f = open('myfile.txt', 'w')
# Open a file for reading and writing
f = open('myfile.txt', 'r+')
# Open a binary file for reading
f = open('myfile.txt', 'rb')
Because read mode ‘r’ and text mode ‘t’ are default modes, you do not need to specify them.
Read a File
Suppose you have the following file.
First line of the file.
Second line of the file.
Third line of the file.
To read its contents, you can use read()
method.
# Read entire file
f = open('myfile.txt')
print(f.read())
# Prints:
# First line of the file.
# Second line of the file.
# Third line of the file.
By default, the read()
method reads the entire file. However, you can specify the maximum number of characters to read.
f = open('myfile.txt')
print(f.read(3))
# Prints Fir
print(f.read(5))
# Prints First
Read Lines
To read a single line from the file, use readline()
method.
f = open('myfile.txt')
print(f.readline())
# Prints First line of the file.
# Call it again to read next line
print(f.readline())
# Prints Second line of the file.
You can loop through an entire file line-by-line using a simple for loop.
f = open('myfile.txt')
for line in f:
print(line)
# Prints:
# First line of the file.
# Second line of the file.
# Third line of the file.
If you want to read all the lines in a file into a list of strings, use readlines()
method.
# Read all the lines in a file into a list of strings
f = open('myfile.txt')
print(f.readlines())
# Prints:
# ['First line of the file.\n', 'Second line of the file.\n', 'Third line of the file.']
Note that list(f)
gives the same result as readlines()
Write a File
Use the write()
built-in method to write to an existing file. Remember that you need to open the file in one of the writing modes (‘w’, ‘a’ or ‘r+’) first.
Write mode ‘w’
In ‘w’ mode the entire contents of the file are overwritten:
f = open('myfile.txt', 'w')
f.write('Overwrite existing data.')
Overwrite existing data.
Append mode ‘a’
In ‘a’ mode, text is added to the end of the file:
f = open('myfile.txt', 'a')
f.write(' Append this text.')
First line of the file.
Second line of the file.
Third line of the file. Append this text.
Read-write mode ‘r+’
In ‘r+’ mode, the file is partially overwritten:
f = open('myfile.txt', 'r+')
f.write('---Overwrite content---')
---Overwrite content---
Second line of the file.
Third line of the file.
Write Multiple Lines
To write multiple lines to a file at once, use writelines()
method. This method accepts list of strings as an input.
f = open('myfile.txt', 'w')
lines = ['New line 1\n', 'New line 2\n', 'New line 3']
f.writelines(lines)
New line 1
New line 2
New line 3
Flush Output Buffer
When you write to a file, the data is not immediately written to disk instead it is stored in buffer memory. It is written to disk only when you close the file or manually flush the output buffer.
# Flush output buffer to disk without closing
f = open('myfile.txt', 'a')
f.write('Append this text.')
f.flush()
Close a File
It’s a good practice to close the file once you are finished with it. You don’t want an open file running around taking up resources!
Use the close()
function to close an open file.
f = open('myfile.txt')
f.close()
# check closed status
print(f.closed)
# Prints True
There are two approaches to ensure that a file is closed properly, even in cases of error.
The first approach is to use the with keyword, which Python recommends, as it automatically takes care of closing the file once it leaves the with
block (even in cases of error).
with open('myfile.txt') as f:
print(f.read())
The second approach is to use the try-finally block:
f = open('myfile.txt')
try:
# File operations goes here
finally:
f.close()
Create a New File
If you try to open a file for writing that does not exist, Python will automatically create the file for you.
# Create a file and open it for writing
# write mode
f = open('newFile.txt', 'w')
# append mode
f = open('newFile.txt', 'a')
# read + write mode
f = open('newFile.txt', 'r+')
There is another way to create a file. You can use open()
method and specify exclusive creation mode ‘x’.
# Create a file exclusively
f = open('newFile.txt', 'x')
Note that when you use this mode, make sure that the file is not already present, if it is, Python will raise the error.
Delete a File
You can delete a file by importing the OS module and using its remove()
method.
import os
os.remove('myfile.txt')
Check if File Exists
If you try to open or delete a file that does not exist, an error occurs. To avoid this, you can precheck if the file already exists by using the isfile()
method from the OS module.
import os
if os.path.isfile('myfile.txt'):
f = open('myfile.txt')
else:
print('The file does not exist.')
Random Access
Sometimes you need to read from the record in the middle of the file at that time you can use the seek(offset, from_what)
method. This function moves the file pointer from its current position to a specific position.
The offset is the number of characters from the from_what parameter which has three possible values:
- 0 – indicates the beginning of the file (default)
- 1 – indicates the current pointer position
- 2 – indicates the end of the file
Suppose you have the following file.
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Here’s how you can use the seek()
method to randomly access the contents of a file.
f = open('myfile.txt', 'rb+')
# go to the 7th character and read one character
f.seek(6)
print(f.read(1))
# Prints G
# go to the 3rd character from current position (letter G)
f.seek(3, 1)
print(f.read(1))
# Prints K
# go to the 3rd character before the end
f.seek(-3, 2)
print(f.read(1))
# Prints X
For Python 3.2 and up, in text files (those opened without a b in the mode string), only seeks relative to the beginning of the file are allowed.
In order to use seek from current position and end, you have to open the text file in binary mode.
If you want to check the current position of the file pointer, use the tell()
method.
f = open('myfile.txt')
# initial position
print(f.tell())
# Prints 0
# after reading 5 characters
f.read(5)
print(f.tell())
# Prints 5