Python File Handling

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.

Example:

f = open('myfile.txt')

When you specify the filename only, it is assumed that the file is located in the same folder as Python.

You can also specify the exact path that the file is located at.

Example: Specifying absolute path

f = open('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:

Python File Modes
CharacterModeDescription
‘r’Read (default)Open a file for read only
‘w’WriteOpen a file for write only (overwrite)
‘a’AppendOpen a file for write only (append)
‘r+’Read+Writeopen a file for both reading and writing
‘x’CreateCreate a new file

You can also specify how the file should be handled.

Python File Modes
CharacterModeDescription
‘t’Text (default)Read and write strings from and to the file.
‘b’BinaryRead 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:

Example: Open a file for reading

f = open('myfile.txt')

is same as

f = open('myfile.txt', 'rt')

Because read mode 'r' and text mode 't' are default modes, you don’t need to specify them.

Example: Open a file for reading and writing

f = open('myfile.txt', 'r+')

Read a File

Assume you have the following file.

myfile.txt

First line of the file.
Second line of the file.
Third line of the file.

To read its contents, you can use read() method.

Example: Read entire file

Code:

f = open('myfile.txt')
print(f.read())

Output:

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.

Example: Read specified number of characters

f = open('myfile.txt')
print(f.read(3))    # Fir

print(f.read(5))    # First

Read Lines

To read a single line from the file, use readline() method.

Example: Read a single line from the file

f = open('myfile.txt')
print(f.readline()) # First line of the file.

Call it again to read next line.

print(f.readline()) # Second line of the file.

You can loop through an entire file line-by-line using a simple for loop.

Example: Read entire file line by line

Code:

f = open('myfile.txt')
for line in f:
    print(line)

Output:

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.

Example: Read all the lines in a file into a list of strings

Code:

f = open('myfile.txt')
print(f.readlines())

Output:

['First line of the file.\n', 'Second line of the file.\n', 'Third line of the file.']

Please note that list(f) gives the same result as readlines()

Write a File

To write to an existing file, use write() built-in method.

Remember! first you need to open the file in one of the writing modes (‘w’, ‘a’ or ‘r+’)

‘w’ overwrites entire file content:

Example: Overwrite entire file content

File Contents:

First line of the file.
Second line of the file.
Third line of the file.

Code:

f = open('myfile.txt', 'w')
f.write('Overwrite existing data.')

New File Contents:

Overwrite existing data.


‘a’ appends text to the end of the file:

Example: Append new text to the end of the file

File Contents:

First line of the file.
Second line of the file.
Third line of the file.

Code:

f = open('myfile.txt', 'a')
f.write(' Append this text.')

New File Contents:

First line of the file.
Second line of the file.
Third line of the file. Append this text.

‘r+’ overwrites the file partially:

Example: Overwrite the file partially

File Contents:

First line of the file.
Second line of the file.
Third line of the file.

Code:

f = open('myfile.txt', 'r+')
f.write('---Overwrite content---')

New File Contents:

---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.

Example: Write multiple lines to a file

File Contents:

First line of the file.
Second line of the file.
Third line of the file.

Code:

f = open('myfile.txt', 'w')
lines = ['New line 1\n', 'New line 2\n', 'New line 3']
f.writelines(lines)

New File Contents:

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 the disk instead it is stored in the buffer memory.

It is written to the disk only when you close the file or manually flush the output buffer.

Example: Flush output buffer to disk without closing

f = open('myfile.txt', 'a')
f.write('Append this text.')
f.flush()

Close a File

The close() function is used to close an open 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!

Example: Close a File

f = open('myfile.txt')
f.close()

# check closed status
print(f.closed)		# True

There are two ways to ensure that a file is closed properly, even in cases of error.

The first way is to use the try-finally block:

Example: Exception handling during file manipulation

f = open('myfile.txt')
try:
    # File operations goes here
finally:
    f.close()

The second way (Recommended) is to use the with statement.

Python recommends to use the with keyword as it automatically takes care of closing the file once it leaves the with block (even in cases of error).

Example: Using with statement

with open('myfile.txt') as f:
    print(f.read())

Create a New File

To create a new file, use open() method and specify exclusive creation mode ‘x’.

Please note that this mode returns an error if the file already exists.

Example: Create a file exclusively

f = open('newFile.txt', 'x')

However, if you try to open a file (that does not exist) for writing using w, a, or r+ mode, Python will automatically create the file for you.

Example: 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+')

Delete a File

You can delete a file by importing the OS module and using its remove() method.

Example: Delete a File

import os
os.remove('myfile.txt')

Check if File Exists

An error will occur if you try to open or delete a file that doesn’t exist.

To check if file exists without getting an error, use isfile() method from OS module.

Example: Check if file exists

import os
if os.path.isfile('myfile.txt'):
	f = open('myfile.txt')
else:
	print('The file does not exist.')	

Random Access

To read from a record in the middle of a file, use 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.

The from_what parameter 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

Example: Random access to a file’s contents

File Contents:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Code:

f = open('myfile.txt')

# go to the 7th character
f.seek(6)
print(f.read(1))    # G

# go to the 3rd character from current position(G)
f.seek(3, 1)
print(f.read(1))    # K

# go to the 3rd character before the end
f.seek(-3, 2)
print(f.read(1))    # X

If you want to check the current position of the file pointer, use the tell() method.

Example: Check current position of the file pointer

File Contents:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Code:

f = open('myfile.txt')

# initial position
print(f.tell())     # 0

# after reading 5 characters
f.read(5)
print(f.tell())     # 5