File Handling in Python

Data Files

There are two types of data files:
1. Text file: Stores data in ASCII or Unicode encoding. Each line ends with a special EOL (End Of Line) character.
2. Binary file: Stores data in the same format without encoding. The lines of data are not delimited with any EOL character.

Opening Files

The open() function is used to open a file. By default, the file is opened in "read" mode.
myfile = open("details.txt")
However, you can also mention the "read" mode while opening a file.
myfile = open("details.txt", "r")
In the above code, "myfile" is the file object or file handle.
To open a file from a specific location:
myfile = open("c:\\Users\\details.txt", "r")
Double backslashes are provided because it is an escape sequence for representing a single backslash. If you want to use single backslash, then use a raw string.
myfile = open(r"c:\users\details.txt", "r")
When the path of the file is not provided, it searches for that file in the current folder in which the python program resides.
While opening the file in "read" mode, if that file isn't available, then it will result in FileNotFoundError.

Following are the various file modes:
Text File ModesBinary File ModesMeaning
"r""rb"Read only operation. The file must exist, otherwise error occurs.
"w""wb"Write operation. If the file doesn't exist, it gets created. Overwrites the existing content.
"a""ab"Write operation. If the file doesn't exist, it gets created. New content is appended to the existing content.
"r+""rb+" or "r+b"Both read and write operations. The file must exist, otherwise error occurs.
"w+""wb+" or "w+b"Both read and write operations. If the file doesn't exist, it gets created.
"a+""ab+" or "a+b"Both read and write operations. If the file doesn't exist, it gets created. If the file exists, new content is appended to the existing content.

Closing Files

In Python, the opened files are automatically closed at the end of the program, but it is always safer to close it yourself. For this task, we use the close() function:
myfile.close()

Reading from Files

The read() function by default reads the entire file:
myfile.read()
But you can also specify the number of bytes to be read:
myfile.read(5)

The readline() function by default reads one line of data from the file, ending the line with '\n' character:
myfile.readline()
Here also, you can specify the number of bytes to be read:
myfile.readline(8)

The readlines() function reads all the lines from a file and returns them as a list of strings:
myfile.readlines()

Writing to Files

The write() function can write a string of data into a file:
myfile.write("hello world")
The writelines() function can write all the string values stored in a list:
myfile.writelines(mylist)

Appending to a File

To avoid overwriting to a file's previous content, we can use the 'a' mode. This mode allows writing to a file without overwriting the previous content. If you want to enable both read and write operations with append, use 'a+' mode.

The flush() function can be used to forcibly write data into a file that has been accumulated in the output buffer:
myfile.flush()

When specifying pathnames, the . means current folder and .. means parent folder.

Reading and Writing Objects to Binary Files

The pickle module in Python can be used to serialize or de-serialize an object. Serializing means storing an object into a Binary file as a byte stream. And de-serializing is the reverse process of serializing.

To import the pickle module:
import pickle

In Python, serializing is known as pickling. Whereas de-serializing is known as unpickling.

The dump() function is used to write an object to a file:
pickle.dump(mylist, myfile)

To read the object stored in the binary file, we use the load() function:
mydata = pickle.load(myfile)

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory