This tutorial explains file handling in Python. It covers the most common file I/O methods for file operations such as open, read, write, tell, seek, remove, etc. By the end of it, you shall be able to use file I/O in your Python programs.
What does file handling mean?
Here is a basic definition of file handling in Python.
A file is a named location on your computer where you store data for later use. It’s like a digital container on your hard drive that can keep information even when the computer is turned off.
In Python, file processing takes place in the following order.
- Open a file that returns a file handle.
- Use the handle to perform read or write actions.
- Close the file handle.
Before you do a read or write operation to a file in Python, you need to open it first. After that as the read/write transaction completes, you should close it to free the resources tied to the file.
In the next sections, we’ll touch upon all the Python file-handling topics one by one. Since it’s an example-driven Python tutorial, so better you open a Python console to test-run the code.
How to use open() file in Python
When we start to work on file I/O in any language, the first task is opening a file. So, for this purpose, Python provides an open() function. It gives you a file handle which is basically an object in Python. You can use it to read or modify the file.
Python open() file method
file_handle = open(file or file path [, mode][, buffering])
Please note that file_handle
is an instance of the built-in file
class in Python. You should prefix it with a dot and place it before any other methods of the file
class when calling them.
It is important to understand the parameters used in open() for file handling in Python.
Also Read: Read/Write to a File in Python
<file or file path>- Represents our target file. It could be a file or file path (a string).
<mode>- It’s an integer representing the file opening mode, check the below table for details. It’s an optional parameter. By default, a file opens in read-only mode giving us access to read data in simple Python string format.
Another one of the common modes is binary mode which deals in byte form. It’s preferable for accessing non-ascii files like an image or object files. See the table in the next section. It lists the available access modes.
<buffering>- Buffering is like a memory holding area that helps work with files faster by storing data temporarily. The buffering value decides how big this storage area is. Its value is -1 for system default, 0 to disable buffering, or a positive value for custom buffer size.
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> | Read-only mode where the file offset stays at the root. |
<rb> | Binary + read-only modes where the offset remains at the root level. |
<r+> | Read + write modes where the file offset is again at the root level. |
<rb+> | Read + write + binary modes where the file offset is again at the root level. |
<w> | Write-only mode that either creates a new file or re-writes an existing file. |
<wb> | For writing in binary form, it has the same behavior as for write-only mode. |
<w+> | For reading and writing, it has the same behavior as for write-only mode. |
<wb+> | Combines read + write + binary modes. Same behavior as for write-only mode. |
<a> | Append mode which sets the offset to EOF. If the file doesn’t exist, then it gets created. |
<ab> | Append + binary modes which set the offset to EOD. If creates the file if it is non-existent. |
<a+> | Combines the append + read + binary modes while keeping the same behavior as for the append mode. |
<ab+> | Combines the append + read + binary modes while keeping the same behavior as for append mode. |
Python file open() example
This code defines a custom Python class, File
which can open files in different modes. It iterates through various modes to demonstrate file opening and provides error handling.
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: fil = file.open(name, mode) if fil: fil.close()
The code showcases a custom file opener class, allowing files to be opened in different modes, with an example of opening a file in read (‘r’), write (‘w’), append (‘a’), and exclusive creation (‘x’) modes.
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 |
---|---|
<file.closed> | Its value is True or False based on the actual state of the file. |
<file.mode> | Provide the mode used to open the file. |
<file.name> | It will return the file name in use. |
<file.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 fob: # Display file information print("File Name:", fob.name) print("File Closed:", fob.closed) print("Mode of Opening:", fob.mode)
File encoding support
Firstly, understanding Python’s encoding is crucial. It’s important because it allows you to write code that efficiently handles file I/O.
In Python 3.x
, there is a clear difference between strings (text) and a byte (8-bits). It states that the char ‘a’ doesn’t represent the ASCII value 97 until you specify it like that. So, while you want to use a file in text mode, then better if you mention the correct encoding type.
Also, since Python stores files as bytes on disk, it becomes necessary to decode the bytes to text while reading and encode them before writing to the file.
For a note, Python enables platform-dependent encoding by default. Hence, if you don’t change it, then it’s set to <cp1252> for Windows and <utf-8> for Linux.
Thus, the documentation says to quote the desired encoding while opening a file in Python. See the Python code snippet.
f = open('app.log', mode = 'r', encoding = 'utf-8')
For note, you should import the <io> module in Python 2.x
to enable the encoding feature. Python 3.x
does it implicitly.
Also Check: Loop Through Files in a Directory using Python
How to use close() 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.
The file’s close() method
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.
a) Calling 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()
b) Enclosing close() inside 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.
c) 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.
How to use write() file in Python
While you get ready to write data to a file, first of all, open it using a mode (read/write/append). View the list of all available file modes here.
You can even do the same using the append mode. Also, if you’ve used the <w> mode, then it’ll erase the existing data from the file. So you must note this fact while you choose it.
The write() file method
Python provides the write() method to write a string or sequence of bytes to a file. This function returns a number, which is the size of data written in a single Write call.
Example: Handling Read/Write Operation to a File in Python
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
How to use read() file in Python
To retrieve information from a file, the very first step is to open it for reading. Subsequently, you can utilize Python’s file-reading methods to extract the data you need.
file.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.
Example: Calling 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)
How to set 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.
Its syntax:
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.
Syntax:
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.
- If
pos
is set to 0, the file pointer will move to the absolute file position given by theoffset
argument. This means it will move to the start of the file ifoffset
is 0, and to a specific position within the file ifoffset
is a non-zero value. - If
pos
is set to 1, the file pointer will moveoffset
bytes from its current position. This can go forward or backward within the file depending on whetheroffset
is positive or negative. - If
pos
is set to 2, the file pointer will moveoffset
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)
How to rename and delete files
While you were using the <read/write> functions, you may also need to either rename or delete files in Python. So, there comes a <os> module in Python, which brings the support of file rename or delete operations.
So, to continue, first of all, you should import the <os> module in your Python script.
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" )
Summarizing file I/O methods
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. |
Before You Leave
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 like and share this tutorial further. You can support us by sharing this post on social media (Linkedin/Twitter).
All the Best,
TechBeamers.