File handling is an important part of programming. File handling is the process of performing file operations on mainly binary or text files. There are mainly two types of operations that can be performed on files: read and write. Each programming language, from low-level languages such as C to high-level languages like Python, has functionalities that help with file handling.
When you think about it, as programmers, we obviously wouldn’t always want to work in those dull compilers. Sometimes we would want our output to be saved somewhere, or we would need to work with some text written in a text file.
In this article, we’re going to dive deep into file handling in Python, understand the ABCs of file handling, and then learn how to modify a text file.
File Handling in Python
Python has a lot of in-built functionalities for file handling. We can write, read, or append to a binary file or a text file. Let’s check out some common functions for file handling in Python.
Opening a file
To open a file, we use the
open() function in Python. The syntax for the open() function is
file = open("myFile.txt")
Closing a file
To close a file, we use the
close() function in Python. The syntax for the close() function is
<file_pointer>.close(<filename>). Closing a file is important as it saves the changes made to the file and frees up the file.
flush() function saves the changes made to the file. The difference between flush() and close() is that flush() doesn’t close the file.
Modes of file access
The operations that can be done on a file are read, write, and append. Read is to read the contents of the file; write is to write something in a file; and append is for adding something at the end of the file. All these operations can be done on both text files and binary files.
List of all file accessing modes in Python
|Syntax for binary files||Syntax for text files||Operation||Explanation|
|rb||r||Read||If the file exists reading the file is allowed. Else I/O error is raised.|
|wb||w||Write||If the file exists all the existing content is erased else a new file is created with the same name.|
|ab||a||Append||Similar to the write mode with a key difference, instead of erasing the previous content new content is added at the end of the file.|
|rb+ or r+b||r+||Read and Write||Allows both write and read and if the file doesn’t exists I/O error is raised.|
|wb+ or w+b||w+||Write and Read||Allows both write and read and if the file doesn’t exist the new file is created with the same name.|
|a+b or ab+||a+||Append and Read||Allows both append ad read and if the file doesn’t exist new file is created.|
Now that we know all the modes of file handling in Python, let’s start implementing them and explore the different functions available for different modes.
Operations on a file
To use the read function, we simply have to use the syntax
Before we start reading a file, we have to write a text file. Let’s create a text file and write some text in it.
We created a text file and wrote some text in it. Now let’s see how we can read it.
file = open("myFile.txt","r") print(file.read())
To read a file, we first have to open it in read mode. We opened the file in read mode, and then we stored the file pointer in a variable named file. Now when we use the read function on the file, we can see that it extracted the entire text from the file line by line and stored it as a single string.
Readlines have a similar syntax to the read function. Unlike read, readlines stores the entire file line-by-line as a list of strings.
file = open("myFile.txt","r") print(file.readlines())
We used the readlines function to read the same text file. This time we can see the output is a list of strings instead of a string.
The write function is similar to the read function, but instead of reading as a string, it writes a string to a file. Let’s try to write the above text file using the write function.
file = open("myFile.txt","w") file.write("Hello everyone!,My name is Kundan.\nWelcome to askPython everyone!") file.close()
The writelines function is similar to the readlines function, but instead of reading as a list of strings, it writes a list of strings to a file. Let’s try to write the above text file using the write function.
file = open("myFile.txt","w") file.writelines(["Hello everyone!,My name is Kundan.\n","Welcome to askPython everyone!"]) file.close()
Python’s “with” statement for file handling is very handy when you have two related operations that you would like to execute as a pair, with a block of code in between. It guarantees to close the file even if an exception occurs.
with open(<filename>, <mode>) as filehandle: file_manipulation_statement
Closing a file
Closing a file is important to save the changes you have made. Not closing a file also takes up your resources for the time being. When a file is opened, the system is also in an inconsistent state.
Related: Learn file modification functions in depth.
What does it mean to modify a text file?
Modifying a file means replacing a piece of text in the file with another piece of text. It’s a combination of read and write operations. To modify a file, we first have to open it, usually in r+ mode. Then find the piece of text we want to modify. Once we encounter the piece of text, we will replace it with the desired text. After that, we’ll close the file.
Modifying a file is an unsafe process. It has security risks and also brings the system into an inconsistent and unstable state. So there’s no direct method for modifying a file in Python. Though we can modify the text in the file using different indirect methods,
seek() method is used to move the file pointer to a particular index. The
seek() method is generally paired up with the
tell() method which in turn gets the position of the file pointer.
seek() method work?
file = open("myFile.txt","r+") text = file.read() print("myFile from index 0:\n",text) file.seek(6) text = file.read() print("myFile from index 6:\n",text)
seek() method can be used to modify a text file. You can move the file pointer to the desired index and replace the text with the replace method.
The fileinput module is used to iterate over the entire file line by line. In case you want to modify an entire line, using the fileinput module is the best option.
How to iterate over the entire file using the fileinput module?
import fileinput for line in fileinput.input('myFile.txt'): print(line,end='') fileinput.close()
splitlines() splits a string of text at the EOL (end of line). After splitting the text, it creates a list of strings for each line. So like the
readlines() function, we can get a list of strings for each line. Then we can replace a line with another using the replace function.
Creating a list of strings using
file = open("myFile.txt","r+") text = file.read() text.splitlines()
Regex is a pattern-matching module. It can be used to check if a particular pattern exists in a given text. It has applications in many fields and can be seen everywhere in the programming world. The most common use of the regex module is email validation. The Regex module has functions that can help us modify text in a file.
Related: Learn about the regex module in depth.
Now let’s implement the above methods to build a function to modify a text file.
Creating a function to modify a particular string in a text file using regex.
import re def modify(filepath, from_, to_): file = open(filepath,"r+") text = file.read() pattern = from_ splitted_text = re.split(pattern,text) modified_text = to_.join(splitted_text) with open(filepath 'w') as file: file.write(modified_text)
The above code contains a function to modify a particular text in a given file. It takes file.path, a string that is supposed to be changed, and a new string as parameters. First, we open the file. Then we read it using the
read() function. Then we split the old string from the text file using the
split() function. Then we join the new string with the remaining text. Then we will write the entire text back into it. Let’s test this function.
file = open("myFile.txt","r+") text = file.read() print("Before modification:",text) file.close() modify("myFile.txt","Kundan","Rohan") file = open("myFile.txt","r+") text = file.read() print("After modification:",text) file.close()
In the above code, we replaced Kundan with Rohan in our previous example file. Yeah! It’s working. Make sure to close the file when you’re done with it to save the changes; otherwise, simply use a with statement to avoid closing the file.
Common errors and troubleshooting during file handling
File handling can prove hard due to all kinds of errors that occur during file handling. It can be really hard to understand why errors occur sometimes. Let’s explore some common errors and the reasons why they may be occurring.
File Not Found error
The “file not found” error is the most common error occurring in file handling. In general, while performing file handling, we use exception handling (try-except block) to deal with this error. Some of the common reasons for the “file not found” error are listed below.
- Most of the time, the path is incorrect. To avoid it, find the file on your PC and right-click on it. Then click copy file path.
- Spelling error in the path of the file or file name. Always check for potential spelling errors.
- Using the wrong extension. Text files have
the.txtextension. Make sure you use the right file extension.
Permission denied occurs when:
- The file is encrypted with a password.
- The file has some administrator security, and you’re not a root user.
- Another program is utilizing the file.
Common mistakes during file handling
- Always remember to close the file when the work is done.
- If you get an “
UnsupportedOperation"error, check if you’re using the right mode to open the file or if you’re using the right function.
- In the case of a syntax error, check for spelling mistakes.
- Make sure you’ve imported the modules you’re using functions from.
File handling is really hard but also an important topic. Learning all this stuff can be overwhelming at times. Even though you know these functions and their workings, there are always chances of errors here and there. Don’t lose hope. Just keep practicing until you are fluent in it. If you encounter an error, check stack overflow, Python’s official documentation, or other articles written by our friends at AskPython. Use any means, but don’t stop at all.
Official Python documentation for file handling.
Stack Overflow answer for the same question.