File handling is a key aspect of Python programming, allowing us to work with files to read, write, and manipulate data stored in them. Python provides built-in functions and methods to perform these operations efficiently.
Table of Contents
What is the file?
A file is a way to store data permanently on a computer. In programming, we use files to save information so it can be accessed later. They are essential for storing data in a lasting and organized manner.
File Handling Modes
Mode | Description | Example Usage |
‘r’ | Read mode (default). Opens a file for reading. If the file does not exist, it raises an error. | file = open(‘data.txt’, ‘r’) |
‘w’ | Write mode. Opens a file for writing and creates a new file if it doesn’t exist. Overwrites the existing content if the file exists. | file = open(‘data.txt’, ‘w’) |
‘a’ | Append mode. Opens a file for writing and creates a new file if it doesn’t exist. Adds new data to the end of the file. | file = open(‘data.txt’, ‘a’) |
‘x’ | Exclusive creation mode. Creates a new file for writing. If the file already exists, it raises an error. | file = open(‘data.txt’, ‘x’) |
‘b’ | Binary mode. Used to read or write binary files (e.g., images, videos). Add ‘b’ to other modes like ‘rb’, ‘wb’, etc. | file = open(‘image.png’, ‘rb’) |
‘t’ | Text mode (default). Used for text files. Add ‘t’ to other modes like ‘rt’, ‘wt’, etc. | file = open(‘data.txt’, ‘rt’) |
‘+’ | Read and write mode. Used with ‘r+’, ‘w+’, ‘a+’ to read and write data in the same file. | file = open(‘data.txt’, ‘r+’) |
Basic File Handling Operations
- Opening a File
- Writing to a File
- Reading from a File
- Appending to a File
- Closing a File
- Using with Statement
Opening a File:
- Before performing any operation (like read or write) on the file, first we must open that file.
- Use the open() function to open a file in the specified mode.
file = open('example.txt', 'r') # Opens file in read mode
Writing to a File:
Use write(str) or writelines(list of lines) to write data.
# Writing to a file without using 'with'
file = open('output.txt', 'w')
# Writing a single string to the file
file.write("This is the first line.\n")
# Writing multiple lines using writelines()
lines = ["This is the second line.\n", "This is the third line.\n", "This is the fourth line.\n"]
file.writelines(lines)
Reading from a File:
Read the content using
- read() : To read total data from the file
- read(n) : To read ‘n’ characters from the file
- readline() : To read only one line
- readlines() : To read all lines into a list
# Reading the file without using 'with'
file = open('output.txt', 'r')
# Reading the entire content of the file
full_content = file.read()
print("Full content:\n", full_content)
# Reset the file pointer to the beginning
file.seek(0)
partial_content = file.read(15) # Reading the first 15 characters
print("\nFirst 15 characters:\n", partial_content)
# Reset the pointer again for further operations
file.seek(0)
first_line = file.readline() # Reading the first line
print("\nFirst line:\n", first_line)
# Reading all lines into a list
file.seek(0)
all_lines = file.readlines()
print("\nAll lines as a list:\n", all_lines)
Explanation of Output:
1. Full content:
Reads the entire file and prints:
This is the first line.
This is the second line.
This is the third line.
This is the fourth line.
2. First 15 characters:
Reads the first 15 characters from the file:
This is the fir
3. First line:
Reads only the first line:
This is the first line.
4. All lines as a list:
Reads all the lines into a list:
['This is the first line.\n', 'This is the second line.\n', 'This is the third line.\n', 'This is the fourth line.\n']
Appending to a File:
- Use append() mode to add new data at the end without overwriting.
file = open('output.txt', 'a')
file.write("Adding new content.") # Adds new content to the file
Closing a File:
- Always close a file after completing operations to free up system resources.
file.close()
Using with Statement:
Ensures the file is properly closed after its suite finishes.
Advantage
- The `with` keyword automatically closes the file after completing all operations, even in case of exceptions.
- This eliminates the need to explicitly close the file, making file handling easier and safer.
with open('example.txt', 'r') as file:
content = file.read()
# File is automatically closed after this block
Pickling and Unpickling Objects in Python
Pickling is the process of serializing an object, while unpickling is the process of deserializing the object to its original state. The pickle module in Python allows us to perform these operations easily.
import pickle
# Define a simple Python object (a dictionary in this case)
data = {
'name': 'Alice',
'age': 30,
'city': 'New York'
}
# Pickling: Writing the object to a file
file = open('data.pkl', 'wb') # Open file in binary write mode
pickle.dump(data, file) # Perform pickling (serialization)
file.close() # Close the file
# Unpickling: Reading the object back from the file
file = open('data.pkl', 'rb') # Open file in binary read mode
loaded_data = pickle.load(file) # Perform unpickling (deserialization)
file.close() # Close the file
# Display the unpickled data
print("Unpickled Data:", loaded_data)
Explanation:
- Pickling:
- The pickle.dump() function writes the state of the object (data) to a binary file (data.pkl).
- Unpickling:
- The pickle.load() function reads the object back from the file and reconstructs it in memory.
Unpickled Data: {'name': 'Alice', 'age': 30, 'city': 'New York'}
Conclusion
File handling in Python is essential for data management and storage. Understanding various modes and operations allows efficient and effective interaction with files, whether for reading, writing, or appending data.
Knowledge Check
Related Article No.4
Check out our Trending Courses Demo Playlist
Data Analytics with Power Bi and Fabric |
Could Data Engineer |
Data Analytics With Power Bi Fabic |
AWS Data Engineering with Snowflake |
Azure Data Engineering |
Azure & Fabric for Power bi |
Full Stack Power Bi |
Kick Start Your Career With Our Data Job
Social Media channels
► KSR Datavizon Website :- https://www.datavizon.com
► KSR Datavizon LinkedIn :- https://www.linkedin.com/company/datavizon/
► KSR Datavizon You tube :- https://www.youtube.com/c/KSRDatavizon
► KSR Datavizon Twitter :- https://twitter.com/ksrdatavizo
Most Commented