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.

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.

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.

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.

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.

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.