4.6 File Handling in Python
File handling is a technique in Python that allows a program to create, open, read, write, append, and close files stored permanently on a computer's storage. It helps in permanently storing and retrieving data even after the program stops running.
Importance of File Handling
- Permanent Storage: Data stays saved even after the program stops.
- Large Data Handling: Can store thousands of records in a file.
- Data Sharing: Files can be transferred between different systems easily.
- Easy Access: Files can be opened, read, updated, and added to whenever needed.
- Organized Storage: Files allow structured data storage in formats like .txt, .csv, .json.
4.6.1 File Modes
Diagram showing the effect of file modes r (read), w (write/overwrite), and a (append) on an existing text file.
Basic File Handling Operations
- 1Open a File — file_object = open("filename", "mode")
- 2Create a File — using open() with 'w' mode creates a new file if it does not exist
- 3Read from a File — use read() to read entire content, or readline() to read one line at a time
- 4Write to a File — use write() function; 'w' overwrites, 'a' appends
- 5Close a File — use close() to free system resources and save changes
file = open("myfile.txt", "w") file.write("Namaste, How are you?") file.close() file = open("myfile.txt", "r") content = file.read() print(content) file.close() file = open("myfile.txt", "a") file.write("\nIts fine...") file.close()
Reading Lines
- readline() — reads one line at a time from a file; each call moves to the next line
- readlines() — reads all lines at once and returns them as a list of strings
4.6.2 Read and Write CSV Files Using the csv Module
A CSV (Comma-Separated Values) file is a simple text file where data is stored in rows and columns, and each value is separated by a comma. Python provides the built-in csv module, with writer() to write data and reader() to read data.
- 1Import the csv module
- 2Open a CSV file in write mode ('w') with newline="" to avoid extra blank lines
- 3Create a writer object using csv.writer(file)
- 4Write header and rows using writerow() method
import csv file = open("students.csv", "w", newline="") writer = csv.writer(file) writer.writerow(["Name", "Age", "Grade"]) writer.writerow(["Ramesh", 14, 8]) file.close() file = open("students.csv", "r") reader = csv.reader(file) for row in reader: print(row) file.close()
4.6.3 File Handling Using the pandas Library
The pandas library makes it simple to work with structured data files by automatically converting them into DataFrames. This is faster and easier, especially for large datasets.
import pandas as pd data = {'Name': ['Ramu', 'Vurka', 'Mahima'], 'Marks': [85, 90, 78]} students = pd.DataFrame(data) students.to_csv("students.csv", index=False) students = pd.read_csv("students.csv") print(students)
Diagram showing how a Python dictionary of data becomes a pandas DataFrame (table) and is saved to/read from a CSV file.
Note: index=False in to_csv() prevents pandas from adding an extra index column (0,1,2...) when saving the file.
Useful Functions After Reading a CSV with pandas
- students.head(n) — shows first n rows (default 5)
- students.tail(n) — shows last n rows (default 5)
- students.shape — shows number of rows and columns
- students.columns — lists column names
- students['ColumnName'].mean() — calculates average of a column