An Intensive Look at Python File Handling Operations with Hands-on Examples:
In the series of Python tutorial for beginners, we learned more about Python String Functions in our last tutorial.
Python provides us with an important feature for reading data from the file and writing data into a file.
Mostly, in programming languages, all the values or data are stored in some variables which are volatile in nature.
Because data will be stored into those variables during run-time only and will be lost once the program execution is completed. Hence it is better to save these data permanently using files.
Table of Contents:
How Python Handle Files?
If you are working in a large software application where they process a large number of data, then we cannot expect those data to be stored in a variable as the variables are volatile in nature.
Hence when are you about to handle such situations, the role of files will come into the picture.
As files are non-volatile in nature, the data will be stored permanently in a secondary device like Hard Disk and using python we will handle these files in our applications.
Are you thinking about how python will handle files?
Let’s take an Example of how normal people will handle the files. If we want to read the data from a file or write the data into a file, then, first of all, we will open the file or will create a new file if the file does not exist and then perform the normal read/write operations, save the file and close it.
Similarly, we do the same operations in python using some in-built methods or functions.
Types Of File in Python
There are two types of files in Python and each of them are explained below in detail with examples for your easy understanding.
They are:
- Binary file
- Text file
Binary files in Python
Most of the files that we see in our computer system are called binary files.
Example:
- Document files: .pdf, .doc, .xls etc.
- Image files: .png, .jpg, .gif, .bmp etc.
- Video files: .mp4, .3gp, .mkv, .avi etc.
- Audio files: .mp3, .wav, .mka, .aac etc.
- Database files: .mdb, .accde, .frm, .sqlite etc.
- Archive files: .zip, .rar, .iso, .7z etc.
- Executable files: .exe, .dll, .class etc.
Recommended Reading =>> How to Open .7z File
All binary files follow a specific format. We can open some binary files in the normal text editor but we can’t read the content present inside the file. That’s because all the binary files will be encoded in the binary format, which can be understood only by a computer or machine.
For handling such binary files we need a specific type of software to open it.
For Example, You need Microsoft word software to open .doc binary files. Likewise, you need a pdf reader software to open .pdf binary files and you need a photo editor software to read the image files and so on.
Text files in Python
Text files don’t have any specific encoding and it can be opened in normal text editor itself.
Example:
- Web standards: html, XML, CSS, JSON etc.
- Source code: c, app, js, py, java etc.
- Documents: txt, tex, RTF etc.
- Tabular data: csv, tsv etc.
- Configuration: ini, cfg, reg etc.
In this tutorial, we will see how to handle both text as well as binary files with some classic examples.
Python File Handling Operations
Most importantly there are 4 types of operations that can be handled by Python on files:
- Open
- Read
- Write
- Close
Other operations include:
- Rename
- Delete
Python Create and Open a File
Python has an in-built function called open() to open a file.
It takes a minimum of one argument as mentioned in the below syntax. The open method returns a file object which is used to access the write, read and other in-built methods.
Syntax:
file_object = open(file_name, mode)
Here, file_name is the name of the file or the location of the file that you want to open, and file_name should have the file extension included as well. Which means in test.txt – the term test is the name of the file and .txt is the extension of the file.
The mode in the open function syntax will tell Python as what operation you want to do on a file.
- ‘r’ – Read Mode: Read mode is used only to read data from the file.
- ‘w’ – Write Mode: This mode is used when you want to write data into the file or modify it. Remember write mode overwrites the data present in the file.
- ‘a’ – Append Mode: Append mode is used to append data to the file. Remember data will be appended at the end of the file pointer.
- ‘r+’ – Read or Write Mode: This mode is used when we want to write or read the data from the same file.
- ‘a+’ – Append or Read Mode: This mode is used when we want to read data from the file or append the data into the same file.
Note: The above-mentioned modes are for opening, reading or writing text files only.
While using binary files, we have to use the same modes with the letter ‘b’ at the end. So that Python can understand that we are interacting with binary files.
- ‘wb’ – Open a file for write only mode in the binary format.
- ‘rb’ – Open a file for the read-only mode in the binary format.
- ‘ab’ – Open a file for appending only mode in the binary format.
- ‘rb+’ – Open a file for read and write only mode in the binary format.
- ‘ab+’ – Open a file for appending and read-only mode in the binary format.
Example 1:
fo = open(“C:/Documents/Python/test.txt”, “r+”)
In the above example, we are opening the file named ‘test.txt’ present at the location ‘C:/Documents/Python/’ and we are opening the same file in a read-write mode which gives us more flexibility.
Example 2:
fo = open(“C:/Documents/Python/img.bmp”, “rb+”)
In the above example, we are opening the file named ‘img.bmp’ present at the location “C:/Documents/Python/”, But, here we are trying to open the binary file.
Python Read From File
In order to read a file in python, we must open the file in read mode.
There are three ways in which we can read the files in python.
- read([n])
- readline([n])
- readlines()
Here, n is the number of bytes to be read.
First, let’s create a sample text file as shown below.
Now let’s observe what each read method does:
Example 1:
my_file = open(“C:/Documents/Python/test.txt”, “r”) print(my_file.read(5))
Output:
Hello
Here we are opening the file test.txt in a read-only mode and are reading only the first 5 characters of the file using the my_file.read(5) method.
Output:
Example 2:
my_file = open(“C:/Documents/Python/test.txt”, “r”) print(my_file.read())
Output:
Hello World
Hello Python
Good Morning
Here we have not provided any argument inside the read() function. Hence it will read all the content present inside the file.
Output:
Example 3:
my_file = open(“C:/Documents/Python/test.txt”, “r”) print(my_file.readline(2))
Output:
He
This function returns the first 2 characters of the next line.
Output:
Example 4:
my_file = open(“C:/Documents/Python/test.txt”, “r”) print(my_file.readline())
Output:
Hello World
Using this function we can read the content of the file on a line by line basis.
Output:
Example 5:
my_file = open(“C:/Documents/Python/test.txt”, “r”) print(my_file.readlines())
Output:
[‘Hello World\n’, ‘Hello Python\n’, ‘Good Morning’]
Here we are reading all the lines present inside the text file including the newline characters.
Output:
Now let’s see some more practical examples of reading a file.
Reading a specific line from a File
line_number = 4 fo = open(“C:/Documents/Python/test.txt”, ’r’) currentline = 1 for line in fo: if(currentline == line_number): print(line) break currentline = currentline +1
Output:
How are You
In the above example, we are trying to read only the 4th line from the ‘test.txt’ file using a “for loop”.
Output:
Reading the entire file at once
filename = “C:/Documents/Python/test.txt” filehandle = open(filename, ‘r’) filedata = filehandle.read() print(filedata)
Output:
Hello World
Hello Python
Good Morning
How are You
Output:
Python Write to File
In order to write data into a file, we must open the file in write mode.
We need to be very careful while writing data into the file as it overwrites the content present inside the file that you are writing, and all the previous data will be erased.
We have two methods for writing data into a file as shown below.
- write(string)
- writelines(list)
Example 1:
my_file = open(“C:/Documents/Python/test.txt”, “w”) my_file.write(“Hello World”)
The above code writes the String ‘Hello World’ into the ‘test.txt’ file.
Before writing data to a test.txt file:
Output:
Example 2:
my_file = open(“C:/Documents/Python/test.txt”, “w”) my_file.write(“Hello World\n”) my_file.write(“Hello Python”)
The first line will be ‘Hello World’ and as we have mentioned \n character, the cursor will move to the next line of the file and then write ‘Hello Python’.
Remember if we don’t mention \n character, then the data will be written continuously in the text file like ‘Hello WorldHelloPython’
Output:
Example 3:
fruits = [“Apple\n”, “Orange\n”, “Grapes\n”, “Watermelon”] my_file = open(“C:/Documents/Python/test.txt”, “w”) my_file.writelines(fruits)
The above code writes a list of data into the ‘test.txt’ file simultaneously.
Output:
Python Append to File
To append data into a file we must open the file in ‘a+’ mode so that we will have access to both the append as well as write modes.
Example 1:
my_file = open(“C:/Documents/Python/test.txt”, “a+”) my_file.write (“Strawberry”)
The above code appends the string ‘Apple’ at the end of the ‘test.txt’ file.
Output:
Example 2:
my_file = open(“C:/Documents/Python/test.txt”, “a+”) my_file.write (“\nGuava”)
The above code appends the string ‘Apple’ at the end of the ‘test.txt’ file in a new line.
Output:
Example 3:
fruits = [“\nBanana”, “\nAvocado”, “\nFigs”, “\nMango”] my_file = open(“C:/Documents/Python/test.txt”, “a+”) my_file.writelines(fruits)
The above code appends a list of data into a ‘test.txt’ file.
Output:
Example 4:
text=["\nHello","\nHi","\nPython"] my_file=open("C:/Documents/Python/test.txt",mode="a+") my_file.writelines(text) print("where the file cursor is:",my_file.tell()) my_file.seek(0) for line in my_file: print(line)
In the above code, we are appending the list of data into the ‘test.txt’ file. Here, you can observe that we have used the tell() method which prints where the cursor is currently at.
seek(offset): The offset takes three types of arguments namely 0,1 and 2.
When the offset is 0: Reference will be pointed at the beginning of the file.
When the offset is 1: Reference will be pointed at the current cursor position.
When the offset is 2: Reference will be pointed at the end of the file.
Output:
Python Close File
In order to close a file, we must first open the file. In python, we have an in-built method called close() to close the file which is opened.
Whenever you open a file, it is important to close it, especially, with write method. Because if we don’t call the close function after the write method then whatever data we have written to a file will not be saved into the file.
Example 1:
my_file = open(“C:/Documents/Python/test.txt”, “r”) print(my_file.read()) my_file.close()
Example 2:
my_file = open(“C:/Documents/Python/test.txt”, “w”) my_file.write(“Hello World”) my_file.close()
Python Rename or Delete File
Python provides us with an “os” module which has some in-built methods that would help us in performing the file operations such as renaming and deleting the file.
In order to use this module, first of all, we need to import the “os” module in our program and then call the related methods.
rename() method:
This rename() method accepts two arguments i.e. the current file name and the new file name.
Syntax:
os.rename(current_file_name, new_file_name)
Example 1:
import os os.rename(“test.txt”, “test1.txt”)
Here ‘test.txt’ is the current file name and ‘test1.txt’ is the new file name.
You can specify the location as well as shown in the below example.
Example 2:
import os os.rename(“C:/Documents/Python/test.txt”, “C:/Documents/Python/test1.txt”)
Before Renaming the file:
After executing the above program
remove() method:
We use the remove() method to delete the file by supplying the file name or the file location that you want to delete.
Syntax:
os.remove(file_name)
Example 1:
import os os.remove(“test.txt”)
Here ‘test.txt’ is the file that you want to remove.
Similarly, we can pass the file location as well to the arguments as shown in the below example
Example 2:
import os os.remove(“C:/Documents/Python/test.txt”)
Encoding in Files
File encoding represents converting characters into a specific format which only a machine can understand.
Different machines have different encoding format as shown below.
- Microsoft Windows OS uses ‘cp1252’ encoding format by default.
- Linux or Unix OS uses ‘utf-8’ encoding format by default.
- Apple’s MAC OS uses ‘utf-8’ or ‘utf-16’ encoding format by default.
Let’s see the encoding operation with some examples.
Example 1:
my_file = open(“C:/Documents/Python/test.txt”, mode=”r”) print(“Microsoft Windows encoding format by default is:”, my_file.encoding) my_file.close()
Output:
Microsoft Windows encoding format by default is cp1252.
Here, I executed my program on the windows machine, so it has printed the default encoding as ‘cp1252’.
Output:
We can also change the encoding format of a file by passing it as arguments to the open function.
Example 2:
my_file = open(“C:/Documents/Python/test.txt”, mode=”w”, encoding=”cp437”) print(“File encoding format is:”, my_file.encoding) my_file.close()
Output:
File encoding format is: cp437
Output:
Example 3:
my_file = open(“C:/Documents/Python/test.txt”, mode=”w”, encoding=”utf-16”) print(“File encoding format is:”, my_file.encoding) my_file.close()
Output:
File encoding format is: utf-16
Output:
Writing and Reading Data from a Binary File
Binary files store data in the binary format (0’s and 1’s) which is understandable by the machine. So when we open the binary file in our machine, it decodes the data and displays in a human-readable format.
Example:
#Let’s create some binary file.
my_file = open(“C:/Documents/Python/bfile.bin”, “wb+”) message = “Hello Python” file_encode = message.encode(“ASCII”) my_file.write(file_encode) my_file.seek(0) bdata = my_file.read() print(“Binary Data:”, bdata) ntext = bdata.decode(“ASCII”) print(“Normal data:”, ntext)
In the above example, first we are creating a binary file ‘bfile.bin’ with the read and write access and whatever data you want to enter into the file must be encoded before you call the write method.
Also, we are printing the data without decoding it, so that we can observe how the data exactly looks inside the file when it’s encoded and we are also printing the same data by decoding it so that it can be readable by humans.
Output:
Binary Data: b’Hello Python’
Normal data: Hello Python
Output:
File I/O Attributes
Attribute | Description |
---|---|
Name | Return the name of the file |
Mode | Return mode of the file |
Encoding | Return the encoding format of the file |
Closed | Return true if the file closed else returns false |
Example:
my_file = open(“C:/Documents/Python/test.txt”, “a+”) print(“What is the file name? ”, my_file.name) print(“What is the file mode? ”, my_file.mode) print(“What is the encoding format? ”, my_file.encoding) print(“Is File closed? ”, my_file.closed) my_file.close() print(“Is File closed? ”, my_file.closed)
Output:
What is the file name? C:/Documents/Python/test.txt
What is the file mode? r
What is the encoding format? cp1252
Is File closed? False
Is File closed? True
Output:
Let’s try out a few other methods of the file.
Example:
my_file = open(“C:/Documents/Python/test.txt”, “w+”) my_file.write(“Hello Python\nHello World\nGood Morning”) my_file.seek(0) print(my_file.read()) print(“Is file readable: ?”, my_file.readable()) print(“Is file writeable: ?”, my_file.writable()) print(“File no:”, my_file.fileno()) my_file.close()
Output:
Hello Python
Hello World
Good Morning
Is file readable:? True
Is file writeable:? True
File no: 3
Output:
Python File Methods
Function | Explanation |
---|---|
open() | To open a file |
close() | Close an open file |
fileno() | Returns an integer number of the file |
read(n) | Reads ‘n’ characters from the file till end of the file |
readable() | Returns true if the file is readable |
readline() | Read and return one line from the file |
readlines() | Reads and returns all the lines from the file |
seek(offset) | Change the cursor position by bytes as specified by the offset |
seekable() | Returns true if the file supports random access |
tell() | Returns the current file location |
writable() | Returns true if the file is writable |
write() | Writes a string of data to the file |
writelines() | Writes a list of data to the file |
Let’s see what we have discussed so far in an end-end program.
Example:
my_file = open("C:/Documents/Python/test.txt", mode="w+") print("What is the file name? ", my_file.name) print("What is the mode of the file? ", my_file.mode) print("What is the encoding format?", my_file.encoding) text = ["Hello Python\n", "Good Morning\n", "Good Bye"] my_file.writelines(text) print("Size of the file is:", my_file.__sizeof__()) print("Cursor position is at byte:", my_file.tell()) my_file.seek(0) print("Content of the file is:", my_file.read()) my_file.close() file = open("C:/Documents/Python/test.txt", mode="r") line_number = 3 current_line = 1 data = 0 for line in file: if current_line == line_number: data = line print("Data present at current line is:", data) break current_line = current_line + 1 bin_file = open("C:/Documents/Python/bfile.exe", mode="wb+") message_content = data.encode("utf-32") bin_file.write(message_content) bin_file.seek(0) bdata = bin_file.read() print("Binary Data is:", bdata) ndata = bdata.decode("utf-32") print("Normal Data is:", ndata) file.close() bin_file.close()
Output:
What is the file name? C:/Documents/Python/test.txt
What is the mode of the file? w+
What is the encoding format? cp1252
Size of the file is: 192
Cursor position is at byte: 36
Content of the file is: Hello Python
Good Morning
Good Bye
Data present at the current line is: Good Bye
Binary Data is: b’\xff\xfe\x00\x00G\x00\x00\x00o\x00\x00\x00o\x00\x00\x00d\x00\x00\x00 \x00\x00\x00B\x00\x00\x00y\x00\x00\x00e\x00\x00\x00′
Normal Data is: Good Bye
Output:
Summary
Enlisted below are a few pointers that can be summarized from the above tutorial:
- We usually use a file for storing data permanently in the secondary storage as it is non-volatile in nature, so that the data may be used in the future.
- At times in some applications we may want to read the data from a text file or binary file, so we can achieve it using the in-built functions in Python like open, read, write methods etc.
- You have to be very careful while using the write method because whatever data you write into the file will be overwritten and the old data will be lost.
- In order, to prevent overwriting of data it’s better to open a file in write and append mode so that data will be appended at the end of the file.
- Remember, when you open a file in the binary mode it does not accept the encoding parameter.
- You can perform renaming and deleting on a file using the rename and remove methods from the “os” module/ package.
We hope you enjoyed this informative tutorial on Python File Handling. Our upcoming tutorial will explain more about Python Main Function.