Introduction
Python file handling allows users to read and write files and perform other file-related operations. Python treats files differently depending on whether they are text or binary. Each line of code contains a sequence of characters that together form a text file, and it is terminated by a special character known as the EOL or End of Line character, which can be a comma or a newline character. Python file must be opened first before performing the read and write operation, and after this, make sure to close the file.
Opening Files in Python
We can open a file with "open('file_name', 'mode')" and when we open the file, we must specify the mode, representing the purpose of the Python open file. The open command will open the file in the read mode only.
Syntax
file_object = open('file_name', 'mode')
The file_name includes the file extension and assumes the file is in the current working directory. If the file location is elsewhere, provide the absolute or relative path.
The mode is an optional parameter that defines the file opening method. It has different possible options described below.
Mode |
Description |
r |
open an existing file for a read operation(Default). |
w |
open an existing file for a write operation. If the file already contains some data, it will be overridden, but if the file is not present, it also creates the file. |
a |
open an existing file for append operation. It won't override existing data. |
r+ |
To read and write data into the file. The previous data in the file will be overridden. |
w+ |
To write and read data. It will override existing data. |
a+ |
To append and read data from the file. It won't override existing data. |
Syntax
# a file named "abc", will be opened with the reading mode.
file = open(r"C:\Users\mohitm\Downloads\abc.txt","r")
# This will print every line one by one in the file
for each in file:
print (each)
Output
Working of read() mode
If we need to extract a string containing all characters in the file, we can use the file.read(). We can also extract a certain number of characters in this method.
Syntax
# Python code to illustrate read() mode
file = open(r"C:\Users\mohitm\Downloads\abc.txt", "r")
print (file.read()) #read all data of file
Output
We can also extract a certain number of characters. The below code will read the first five characters from the file and return it as a string.
Syntax
# Python code to illustrate read() mode
file = open(r"C:\Users\mohitm\Downloads\abc.txt", "r")
print (file.read(7)) #read the first seven characters of stored data and return it as a string
Output
Creating a file using the write() mode
If we want to write into a file in Python, we need to open it in write mode by passing "w" inside open() as a second argument. There are two essential things we need to remember while writing a file. The first one is, in write mode, if we try to open a file that doesn't exist, a new file is created. And second, if a file already exists, its content is erased, and new content is added.
Syntax
# Python code to open a file in write mode
#"abc.txt" file is already available so it will not create new file but it will overwrite its data
file = open(r"C:\Users\mohitm\Downloads\abc.txt", "w")
file.write("This is the write command")
file.write(" It allows us to write in a particular file")
file.close()
Output
Syntax
# Python code to create a file in write mode
#"xyz.txt" file is not available so it will create new file and write the data
file = open(r"C:\Users\mohitm\Downloads\xyz.txt", "w")
file.write("This is the write command")
file.write(" It allows us to write in a particular file")
file.close()
Output
Working of append() mode
Append mode adds information to an existing file. It always places the pointer at the end. If a file does not exist, append mode creates the file.
Syntax
# Python code to illustrate append() mode
#"abc.txt" is already available so it will append the data.
file = open(r"C:\Users\mohitm\Downloads\abc.txt", "a")
file.write("\n")
file.write("This will add this line.")
file.close()
Output
Syntax
# Python code to illustrate append() mode
#"demo.txt" file is not available sp it will create new file and add data on it
file = open(r"C:\Users\mohitm\Downloads\demo.txt", "a")
file.write("This will add this line.")
file.close()
Output
Closing a file
The close() method is used for closing a file in Python. Although it is not mandatory to close a file as Python uses the garbage collector to clean the unreferenced objects.
Syntax
# Python code to illustrate close()
file = open(r"C:\Users\mohitm\Downloads\demo.txt", "a")
file.write("Aappend the text")
file.close()
Conclusion
File handling in Python is a powerful and versatile tool that can perform a wide range of operations. It can be used for manipulating files using the Python programming language. It includes many activities, from reading and writing data to files and much more.