Learn file handling in Python to open, create, read, write, rename, and delete files on your system. Check out this tutorial to perform file operations step by step.

Understand File Handling in Python
A file is a means to store information on your system. In programming, you may encounter different types of files, such as text files (e.g., .txt or .csv) and binary files (e.g., .pdf). To effectively manage file handling in Python, follow the steps below.
Open a file in Python
To open a file in Python, first, you can call the Python’s built-in open() function. It’s syntax is given below.
file_handle = open(file or file path [, mode][, buffering])
It is important to understand the parameters used in open() for file handling in Python.
The open() function typically takes two arguments: the file name with or without the path and the mode in which to open the file. Upon successful execution, it returns a file handle, which can be used for further operations.
Regarding buffering, it refers to the temporary storage of data during file operations to enhance performance. The default buffer size is 8,192 bytes (8 KB), meaning Python reads or writes data in chunks of this size.
You may also like: Read/Write to a File in Python
File open modes in Python
Please check the below list of modes available in Python for handling files. It is mandatory to specify a mode and all the below modes open a file for any further action.
Modes | Description |
---|---|
r | Opens a file for reading (default mode). |
rb | Same as above (but in binary format). |
r+ | Opens a file for both reading and writing. |
rb+ | Same as above (but in binary format). |
w | Opens a file for writing. Creates a new file or truncates existing content. |
wb | Same as above (but in binary format). |
w+ | Opens a file for both writing and reading. Creates a new file or truncates existing content. |
wb+ | Same as above (but in binary format). |
a | Opens a file for appending. Creates a new file if it doesn’t exist. |
ab | Same as above (but in binary format). |
a+ | Opens a file for both appending and reading. Creates a new file if it doesn’t exist. |
ab+ | Same as above (but in binary format). |
Python file open() example
The example below shows how to get started with file handling in Python.
It start by defining a custom Python class, that opens files in different modes. The program then iterates through various file-opening modes with error handling in place.
import io
class File(io.IOBase):
def open(self, name, mode='r', **kwargs):
try:
f = io.open(name, mode, **kwargs)
print(f"Opened '{name}' in mode '{mode}'")
return f
except IOError:
print(f"Error opening '{name}' in mode '{mode}'")
file = File()
name = "example.txt"
modes = ["r", "w", "a", "x"]
for mode in modes:
handle = file.open(name, mode)
if handle:
handle.close()
Python file handle and its attributes
The file handle in Python is an object which is returned by the open() call. Interestingly, this handle has many properties or attributes to give you info like metadata.
For example, by using it, you can check whether the file is closed, which mode was it opened, what is its name, and some more things like that. Take a look at the below table for more insights.
Attribute | Description |
---|---|
handle.closed | Its value is True or False based on the actual state of the file. |
handle.mode | Provide the mode used to open the file. |
handle.name | It will return the file name in use. |
handle.softspace | A Boolean to check if space was added or not before printing another value. Note: Removed in Python 3. |
Must Check – Multiple Ways to Copy a File in Python
The following code lists the different attributes of a file handle in Python.
# In this code, we open "app.log" in wb (write + binary) mode
# and printing their values by using the context manager
with open("app.log", "wb") as handle:
# Show file info
print("File Name:", handle.name)
print("File Closed:", handle.closed)
print("Mode of Opening:", handle.mode)
File encoding in Python
When working with file handling in Python, you should be mindful of encoding. By default, Python uses the system’s default encoding, which can cause inconsistencies when running your code on different platforms. To avoid such issues, it’s best to specify the encoding explicitly.
You can use the encoding parameter in the open() function to define the desired encoding, such as UTF-8.
# Reading a file with UTF-8 encoding
with open('example.txt', 'r', encoding='utf-8') as file:
content = file.read()
# Writing to a file with UTF-8 encoding
with open('example.txt', 'w', encoding='utf-8') as file:
file.write('Hello, World!')
Also Check: Loop Through Files in a Directory using Python
Close a file in Python
The best practice in Python is to close the file handle when you’re done with it, even though Python’s garbage collector (GC) cleans up unused objects. As the owner of the code, make sure to close the file, rather than relying on GC to do it for you.
Python offers the file_handle.close() method for shutting down a file. When you close a file, the system releases all the allocated resources, and this process is quite straightforward to execute. Please see the below code snippets.
Simple close() in Python
The following code demonstrates the standard way to close a file once you no longer need it.
f = open("app.log",encoding = 'utf-8') # do file operations. f.close()
Enclosing close() in a try block
Say, for example, your code may fail or throw some error while making some I/O calls. In such a case, the code would actually exit without closing the file. So it’s better to put the code inside a <try-finally> block.
try: fl = open('apptest.log', encoding = 'utf-8') # Make some file I/O class like read or write. finally: fl.close()
By using a try block in your code, you can effectively handle any file access errors. It will also ensure to close the file even if any error occurs.
Replace close() with auto close
Another way to close a file is by using the WITH clause. It keeps track of all the calls made inside the With block and auto-closes the file at the end.
with open('apptest.log', encoding = 'utf-8') as f: #do any file operation.
Write to a file in Python
The write() function in Python allows you to write a string or a sequence of bytes to a file. It returns the number of characters (or bytes) written in a single call.
file_handle.write(data)
Below is an example demonstrating how to use write() to store data in a file.
with open('app.log', 'w', encoding = 'utf-8') as f: #first line f.write('my first file\n') #second line f.write('This file\n') #third line f.write('contains three lines\n') with open('app.log', 'r', encoding = 'utf-8') as f: content = f.readlines() for line in content: print(line)
Python 3.5.1 [GCC 4.8.2] on Linux my first file This file contains three lines
Also Check: Read File Line by Line in Python
Read a file in Python
The next built-in function you need for file handling in Python is read(). It allows you to extract data from a file efficiently.
file_handle.read(size=-1)
It reads and returns a specified number of bytes (given by size) from the file. If size is not provided, or if it’s negative (the default), the entire content of the file is read.
Check the example below on how to call read() to fetch data in different sizes.
# Open 'app.log' in write mode with UTF-8 encoding with open('app.log', 'w', encoding='utf-8') as f: # Write three lines of text to the file f.write('my first file\n') # 1st line f.write('This file\n') # 2nd line f.write('contains three lines\n') # 3rd line # Open 'app.log' in read mode with open('app.log', 'r', encoding='utf-8') as f: # Read the first 10 chars from the file first_10_chars = f.read(10) # Output: 'my first f' # Read the next 4 characters next_4_chars = f.read(4) # Output: 'ile\n' # Read the rest of the file until the end rem_text = f.read() # Output: 'This file\ncontains three lines\n' # Further attempts to read return an empty string empty_result = f.read() # Output: '' # Display the results print(first_10_chars) print(next_4_chars) print(rem_text) print(empty_result)
File offset in Python
The following are the two important methods to set the offset for file handling in Python.
Tell() method
This method measures and returns where is the file pointer currently in a file.
cur_pos = file.tell()
The tell() method is free from any argument. It simply returns the current file position as an integer. This value represents the byte offset from the start of the file. Here’s an example of how to use the tell method:
# Open a file in binary mode file = open("example.txt", "rb") # Get the current file position position = file.tell() print(f"Current file position: {position}") # Read some data from the file data = file.read(10) # Get the updated file position after reading position = file.tell() print(f"Updated file position: {position}")
Seek() method
The purpose of the seek() method is to shift the location of the file pointer within a file.
file.seek(offset[, pos])
- The offset is like how much we move.
- The pos is where we begin from.
Here are some key points about its parameters.
- The file pointer will move to the start of the file if the offset is 0, and to a specific position within the file if offset is a non-zero value.
- If pos is set to 1, the file pointer will move by the offset bytes from its current position. This can go forward or backward within the file depending on whether offset is positive or negative.
- If pos is set to 2, the file pointer will move offset bytes from EOF. To position the file pointer at the EOF, set the offset to 0.
Here’s an example of how to use the seek method:
# Open a file in binary mode file = open("example.txt", "rb") # Move to the start of the file file.seek(0, 0) # Move 10 bytes from the current position (forward) file.seek(10, 1) # Move 20 bytes from the end of the file (backward) file.seek(-20, 2)
Rename and delete files in Python
While using the read/write functions, you may also need to rename or delete files in Python. This can be done using the OS module. To use its functions, you must first import it into your Python program.
Here are some useful functions from the OS module:
Rename() file method
os.rename(cur_file, new_file)
In the <rename()> method, we need to pass the name of an old file and a new name.
The following Python code renames the old file with a new name, for example, the file <app.log> to <app1.log>.
Example:
import os #Rename app.log to app1.log os.rename( "app.log", "app1.log" )
Remove() file method
os.remove(file_name)
The <remove()> method deletes a file that it receives in the argument.
The below Python code removes the given file, for example, the <app.log>.
Example:
import os #Removing a file <app1.log> os.remove( "app1.log" )
Summary: File Handling in Python
So far, we’ve only shared with you a few of the functions that you can use for file handling in Python. But there is more to the story of Python file handling.
Python’s open() method returns an object which we call the file handle. Python adds a number of functions that we can call using this object.
It is important to realize that you call all the below functions by prefixing them with the file object and a dot. However, you may also use any other custom name that you like to assign to it. This file is not exactly a Python keyword but a special built-in name.
Names | Purpose |
---|---|
close() | Close a file and free any resource bound to it. |
flush() | Make sure any unsaved data in a file is written to the file right away. |
fileno() | Provide a number representing the file identifier. |
isatty() | Return True if the file is connected to a terminal or tty, False otherwise. |
next() | Removed in Python 3. |
read(num) | Read a specified number of bytes from a file. |
readline(num) | Read and return a single line, up to the specified number of bytes. |
seek(offset[, pos]) | Change the position of the file pointer within an open file. |
tell() | Return the current position of the file pointer within an open file. |
truncate(num) | Change the size of a file by resizing it. |
write(str) | Write the contents of a string to an open file. |
writelines(seq) | Write strings from the seq. on separate lines without adding line breaks. |
We hope this tutorial made it easy for you to learn file handling in Python. If you want us to bring more such tutorials, then subscribe to our YouTube channel.
Happy Coding,
TechBeamers