Python Tutorial | Python Data File Handling

 

What is a file?

A file is a document or the data stored on a permanent storage device that can be read, written, or rewritten according to requirement.
or
data is packed up on the storage device as data structures called files. All the files are assigned a name that is used for identification purposes by the operating system and the user.
or
The 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).

Why use Files

Data maintained inside the files are termed as persistent data i.e. it is permanent in nature. Python allows us to read data from and save data to external text files permanently on secondary storage media. Apart from this we also store program data as a python script (.py extension).

Data File Operations

Before we start working with a file, first we need to open it. After performing the desired operations, it needs to be closed so that the resources that are tied with the files are freed or released.
Hence, in Python, a file operation takes place in the following order.

1.Open a file
2.Performing operations/ processing data (Read or write etc. )
3.Close the file

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 the 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:

Binary files are used to store binary data such as images, video files, audio files, etc. A binary file contains binary data, usually numbers stored in files that can be used for numerical operations. So when we work in binary files, we have to interpret the raw bit pattern read from the file into the correct type of data in our program.

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 files. 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 don’t need to be read by people or need to be ported to a different type of system, binary files are the best way to store the program.

Operations on 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 ModeDescription
rOpens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
rbOpens 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.
wOpens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
wbOpens 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.
aOpens 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.
abOpens 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

Python Basic Programming Tutorial

Python Introduction     Getting started in Python Programming      Python propgramming fundamentals     Python Operators    Python If Condition     Python for loop    Python range construct      Python While loop    break and continue statements     Different looping techniques     Python List     Python String     Python Functions    Python Inbuilt Functions     Python Recursion     Using Python Library     Python Tuples     Python Dictionary     Python Sets     Python Strings     Python Exception Handling     Python Data File Handling

Tutorials

Technical Questions and Important programs

Interview Questions

C Programming
C++ Programming
Basic Python Tutorial
Advanced Python Tutorial
C Language
C++ Programming
Python programming
C Important Programs
C Interview Questions
C++ Interview Questions
Python Interview Questions
HTML Interview Questions