Feb 27, 2023 6 min read

File Handling in Python

Handle files in Python with this beginner-friendly tutorial. Explore reading and writing files, manipulating file content, and more.

File Handling in Python
File Handling in Python
Table of Contents

Introduction

File handling is an essential component of programming. Handling files is made simpler by Python's built-in functions for generating, opening, and closing files.

Python also enables performing several file operations, such as reading, writing, and appending information, while files are open.

Prerequisites

  • Python 3 is installed and configured.
  • A code editor to write code or an IDE (Integrated Development Environment).
  • Access to a terminal to execute the code (or run directly in an IDE).
  • A text file for the examples.

Opening Files in Python

The basic function for handling files in Python is the open() method. The general syntax is:

file_object = open('file_name', 'mode')

For file handling, the open() function requires two basic parameters:

  1. The file_name presumes the file is in the current working directory and includes the file extension. Provide the absolute or relative path if the file is located elsewhere.
  2. The mode specifies the file opening method and is an optional parameter. The many options are listed in the table below:
Mode Description
'r' Reads from a file and, if the file is not present, returns an error (default).
'w' Writes to a file and creates the file if it does not exist or overwrites an existing file.
'x' Exclusive creation that is unsuccessful if the file is already present.
'a' Appends to a file, creating it if it does not already exist, or overwriting an existing file.
'b' Binary mode. Use this mode for non-textual files, such as images.`
't' Text mode. Use only for textual files (default).
'+' Activates read and write methods.

The mode must have precisely one create(x)/read(r)/write(w)/append(a) method, at most one +. Text files are read using 'rt' when the mode is omitted.

The operations of each mode when it is invoked are described in the table that follows:

Behavior Modes
Read r, r+, w+, a+, x+
Write r+, w, w+, a, a+, x+`
Create w, w+, a, a+, x, x+
Pointer Position Start r, r+, w, w+, x, x+
Pointer Position End a, a+
Truncate (clear contents) w, w+`
Must Exist r, r+
Must Not Exist x, x+


Read Mode

Python's read mode opens an existing file for reading when the pointer is at the beginning of the file.

💡
Note: Python throws an error if the file does not exist.

In order to read a text file in Python, load the file using the open() function:

f = open("<file name>")

The default mode is read text, or 'rt' Therefore, the following method is the same as the default:

f = open("<file name>", "rt")

Use the following to read files in binary mode:

f = open("<file name>", "rb")

To open a file in read-write mode, add +:

f = open("<file name>", "r+")  # Textual read and write
f = open("<file name>", "rt+") # Same as above
f = open("<file name>", "rb+") # Binary read and write

The function always returns a file object, and the characteristics vary depending on the selected mode.

💡
Note: For more information on using stdin to read files, see our article How to Read From Stdin in Python.

Write Mode

When in write mode, a file is created for writing content, and the pointer is moved to the beginning. If the file already exists, write truncates (clears) all existing data in it.

⚠️
Warning: The write mode immediately deletes any existing content. Before overwriting data by accident, check to see if a file already exists.

To open a file for writing information, use the following line:

f = open("<file name>", "w")

Since the default mode is text, the below line is equivalent to the default:

f = open("<file name>", "wt")

Open the file with the below line to write in a binary mode:

f = open("<file name>", "wb")

To allow reading the file, add +:

f = open("<file name>", "w+")  # Textual write and read
f = open("<file name>", "wt+") # Same as above
f = open("<file name>", "wb+") # Binary write and read

The open() function returns a file object whose details are determined by the modes chosen.

Append Mode

In the append mode, information is added to an existing file, and the pointer is placed at the end. In the event that a file is missing, append mode creates one.

💡
Note: The main distinction between write and append modes is that append does not remove the contents of a file.

Use one of the following lines to open a file in append mode:

f = open("<file name>", "a")  # Text append
f = open("<file name>", "at") # Same as above
f = open("<file name>", "ab") # Binary append

Add the + sign to enable the read functionality.

💡
Note: Understand how to append a string in Python.

Create mode (also known as exclusive create) places the pointer at the beginning of the file and only creates a file if one does not already exist.

💡
Note: Python throws an error if the file already exists. By using this mode, you can avoid overwriting existing files.

To open a file in create mode, use one of the below lines:

f = open("<file name>", "x")  # Text create
f = open("<file name>", "xt") # Same as above
f = open("<file name>;", "xb") # Binary create

To include functionality to any of the above lines, add the + sign to the mode.

Reading Files in Python

Python provides many ways to read the contents of a file after importing it into an object.

Call the read() method on the file object and output the result. For instance:

f = open("file.txt")
print(f.read(),end="")
💡
Note: A new, empty line is added by default when using the print() function. Add the end="" option to print() in order to get rid of the blank line.

The code prints the contents of the text file.

Read Parts of the File

To read only the given number of characters, pass a number to the read() function:

f = open("file.txt")
print(f.read(5))

The first five characters of the file are printed in the output.

If you want to print just the file's first line, you can do it by using the readline() method:

f = open("file.txt")
print(f.readline())

To print the given number of characters without exceeding the first line, add an integer to the readline() function.

Read Lines

A for loop is used to read lines and iterate through the contents of a file:

f = open("file.txt")
for line in f:
    print(line, end="")

If you want, you can also use the readlines() method on the file object:

f = open("file.txt")
print(f.readlines())

The function returns a list of the file stream's lines.

To control the number of lines, add an integer to the readlines() function. For instance:

f = open("file.txt")
print(f.readlines(15))

The character number is represented by the integer, and the function returns the line where the character ends, together with the previous lines.

Close Files

A file is open until the close() method is used. To prevent unpredictable file behavior and corrupted files, it is a good idea to close any open files.

Use the close() method on the file object to close a file:

f.close()

Alternatively, you can use the with statement to ensure that a file closes. For instance:

with open("<file name>"):
    file_contents = f.read()
    # Additional code here

The with statement closes the file automatically.

💡
Note: There are three ways to find the length of a list in Python.

Deleting Files in Python

In order to remove files in Python, you must establish communication with the operating system. Import the os library and delete the following file:

import os
os.remove("file.txt")

The file is no longer accessible. Python generates an error if the file does not exist.

Python File Methods

When working with file objects, Python provides a number of additional functions. There is a table below that lists all the processes that are available and what they perform.

Method Description
close() Flushes and closes the file object.
detach() Separates buffer from text stream and returns the buffer.
fileno() Returns the file's descriptor if available.
flush() Flushes the write buffer. Not available for read-only objects.
isatty() Checks if a file stream is interactive.
read() Read number of characters at most.
readable() Checks if an object is readable.
readline() Reads from the object until a newline or end of the file.
readlines() Returns a list of lines from the file object, where is the approximate character number.
seek(, ) Changes the pointer position to relative to the .
tell() Prints the current stream position.
truncate() Resizes the file stream to (or current position if unstated) and returns the size.
write() Writes to the file object and returns the written number of characters.
writable() Checks whether the file object allows writing.
writelines() Writes a of lines to the stream without a line separator.

Conclusion

After following this tutorial, you should be able to handle files in Python. To work with additional file types, try using a Python library like Pandas.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.