File Handling in Python(Text &Binary File)
File Handling in Python(Text &Binary File)
File handling is the process of saving data in a file using Python program. The Python file can
be stored in a text file or in a binary file. There are six different types of modes available in
the Python programming language which is used for reading, writing, and appending files in
Python.
Note: The Python file can be stored in a text file or in a binary file.
For reading, writing, or editing a file in Python, you have to open the file first using
the open() method.
Syntax:
When the file is opened in write mode and if a new file does not exist in the specific location
on the disk, then the write mode creates the file automatically; otherwise, if it is found in
the location, then the same file will be used. The function used to write in the file
is filename.write().
Syntax:
The read() function is used to read content from a text file. It is basically used when we want
to fetch data from a file that is opened in read mode (‘r).
Syntax:
file_object.read()
print(content)
The close() method is important to close the operation on the file after execution. It defines
that the file is properly closed after performing the operation, like reading, writing, or
appending, which will help to prevent the file corruption.
Syntax:
close()
file.write("Hello, World!")
content = file.read()
print(content)
The reading of the text file can be done using 3 different methods as per the requirement.
3. readlines(): Reads all lines from the file and returns them as a list.
1. read() method
The read() method is used to read the entire content from a file; it can handle large files but
takes lots of memory. The read() method can be used in text files and binary files both such
as ‘r’ (text read mode) or ‘rb’ (binary read mode).
Example,
2. readline() method
Reads one line from the file at a time. This method is useful for reading files line by line,
especially in loops.
Example,
3. readlines() method
Reads all lines in the file and returns them as a list of strings; each string in the list ends with
a newline (\n). Ideal for small to medium sizes of files.
Example,
The writing of the content on the text can be done using two methods:
• write(): Writes a single string to a file at a time; does not add newline characters
automatically.
• writelines(): Writes multiple strings to a file; does not add newline characters
automatically.
1. write() method
Writes a single string to a file at a time; does not add newline characters automatically.
2. writeline() method
Writes multiple strings to a file; does not add newline characters automatically.
lines = [
file.writelines(lines)
Appending into a Text File
Append mode helps to add new data in the file without erasing the previous one; this new
data is added at the end of the existing data. To use this append mode, you need to set “a”
as append in the open() function.
file.write("Hello, World!\n") # The new data will be added at the end of the existing
data.
Binary files are made up of non-human-readable data in the form of bytes. The pickle
module is used to convert Python objects like lists, tuples, and dictionaries into a byte
stream called pickling or serialization using the dump() method. To restore or to retrieve the
data in original form is known as unpickling, which can be done using load() method.
The dump() function is used in Python for writing an object into a binary file.
import pickle
data = {
"name": "Anil",
"age": 25,
The load() method is used to read the binary file in Python. This method unpickles the data
from the binary file, which helps to convert the binary stream into the original object.
print(data)