Class XII: Python Data File Handling

What is a file?

File is a named location on disk to store related information. It is used to permanently store data in non-volatile memory (e.g. hard disk).
or
File is a document that stores data on a permanent storage device that can be read, written, or rewritten according to requirement. 

Since random access memory (RAM) is volatile which loses its data when the computer is turned off, we use files for future use of the data.

All the files are assigned a name that is used for identification purpose for the operating system and users.

File types

Before we discuss the file operations, we should be aware of the file types. Python allows us to create and manage two types of files.
• Text files
• Binary Files

Text files

A text file consists of a sequence of lines. A line is a sequence of characters (ASCII or UNICODE), stored on permanent storage media. Although the default character coding in python is ASCII. Using the constant “u” with string, it supports Unicode as well.
In-text file, each line of text is terminated with a special character known as EOL (end of line) character. In-text files, some internal translation take place when this EOL character is read or written. By default, this EOL character is the newline character(‘\n’). So at the lowest level, a text file will be a collection of bytes. Text files are stored in human-readable form and can be created using any text editor.
We use text files to store character data. For example test.txt, abc.txt, etc.

Binary File

A binary file is just a file that contains information in the same format in which the information is held in the memory. In binary file, there is no delimiter for a line. Also no translation occurs in binary file. As a result the binary files are faster and easier for a program to read and write than are the text files. As long as the files doesn’t need to be read by people or need to be ported to a different type system, binary files are the best way to store the program.

Data File Operations

Whenever we want to deal with files ie. when we want to read from or write to a file we need to open it first. When we are done, it needs to be closed, so that resources that are tied with the file are freed.

Hence, in Python, a file operation takes place in the following order.

  1. Open a file
  2. Read or write (perform operation)
  3. Close the file

Opening a file

When we want to read or write a file we must first open the file. Opening the file communicates with the operating system, which knows where the data for each file is stored.
When we open a file, we are asking the operating system to find a file by name and make sure that the file exists.
Open():
Open() function helps us to open a file. It takes two arguments:

1. Name of the file to open
2. Mode of accessing the file (optional by default mode is read mode)

This function open() returns a file object, also called a handle, as it is used to read or modify the file accordingly.

Syntax:
File_variable/file_handle=open(“filename”,”mode”)

Here:

File_variable/file_handle:

If the opening of the file is successful, the operating system returns us a filehandle. The handle is not the actual data stored in the file, instead it is a handle that we can use to read data from the file.
While opening a file a file_handle is associated with the file and then reading or writing operation on the file is carried out using this file_handle. (file name is not used)

Filename:

name of the file storing the data

Mode:

mode helps to specify the purpose for which the file is opened. The mode can be :
read(r): to read the file
write(w): to write contents in the file
append(a): to add data into an existing file after the existing contents.

f = open(“abc.txt”) # open file in current directory
f = open(“E:/Python/abc.txt”) # specifying full path

We can specify the mode while opening a file. In mode, we specify whether we want to read ‘r’, write ‘w’ or append ‘a’ to the file. We also specify if we want to open the file in text mode or binary mode. The default is reading in text mode. In this mode, we get strings when reading from the file. On the other hand, the binary mode returns bytes and this is the mode to be used when dealing with non-text files like image or exe files.

File Modes:

Mode Details
r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
r+ Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
rb+ Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file.
w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
wb Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
wb+ Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
a Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
ab Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
ab+ Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing

How to close a file Using Python?

When we are done with operations to the file, we need to properly close the file.
Closing a file will free up the resources that were tied with the file and is done using Python close() method. Python has a garbage collector to clean up unreferenced objects but, we must not rely on it to close the file.

f = open(“test.txt”)
# perform file operations
f.close()

This method is not entirely safe. If an exception occurs when we are performing some operation with the file, the code exits without closing the file.

A safer way is to use a try…finally block.

try:
    f = open(“test.txt”)
    # perform file operations
finally:
    f.close()

This way, we are guaranteed that the file is properly closed even if an exception is raised, causing the program flow to stop.

The best way to do this is by using “with” statement. This ensures that the file is closed when the block inside “with” is exited.

We don’t need to explicitly call the close() method. It is done internally.

with open(“test.txt”) as f:
       # perform file operations