Back to CS Curriculum

File Handling in Python

CBSE Class 12 - Computer Science

File handling is a crucial skill in Python programming that allows you to store, retrieve, and manipulate data persistently. Learn how to work with text files, binary files, and CSV files to manage data effectively in your programs.

Chapter 1: Understanding Files

What is a File?

A file is a sequence of bytes stored on a disk or any permanent storage medium, where related data is kept. Files are created for the permanent storage of data, allowing it to be retrieved or manipulated later.

File Handling in Python enables us to create, update, read, and delete files stored on the file system through our Python program.

Types of Files

Text Files

These files contain human-readable characters.

Examples:

  • • .txt files
  • • Python scripts (.py)
  • • CSV files
  • • HTML files

Binary Files

These files contain data in binary form and are not human-readable.

Examples:

  • • .dat files
  • • Image files (.png, .jpg)
  • • Audio files (.mp3, .wav)
  • • Video files (.mp4, .avi)

Text Files vs Binary Files

Text FilesBinary Files
Store information in ASCII charactersUsed to store binary data (images, audio, video, text)
Each line terminated with EOL (End of Line)No delimiter used; data is continuous
Easy to understand (human-readable form)More difficult to understand (not human-readable)
Slower to access and processFaster to access and process (closer to hardware format)
Typically have .txt extensionOften have .dat or binary-specific extension

Chapter 2: Opening Files - Two Methods

Two Methods to Open a File

1. Using open() Function

Opens a file by specifying filename and mode. Requires manual closing.

2. Using with Statement

Recommended method - automatically closes file even if exceptions occur.

Benefits of Using with Statement

  • Automatic Closure: File is closed automatically after the block executes
  • Exception Handling: Manages exceptions, ensuring no resource leaks
  • Best Practice: Recommended approach for file handling in Python
# Example:
with open("Output.txt", "w") as f:
f.write("Text")
# File automatically closed

Chapter 3: File Access Modes

Text File Modes

rRead only (default mode)

Opens file for reading. File must exist.

r+Read and write

Opens file for both reading and writing. File pointer at beginning.

wWrite only

Opens file for writing. Overwrites if exists, creates if doesn't exist.

w+Write and read

Opens file for both reading and writing. Overwrites existing file.

aAppend only

Opens file for appending. File pointer at end. Creates file if doesn't exist.

a+Append and read

Opens file for both appending and reading. File pointer at end for writing.

Binary File Modes

rb - Read binary
rb+ - Read/write binary
wb - Write binary
wb+ - Write/read binary
ab - Append binary
ab+ - Append/read binary

Comparison: r+ vs w+ vs a+

Featurer+ Modew+ Modea+ Mode
PurposeRead and writeRead and writeRead and write
File PointerAt beginningAt beginningAt end (for writing), use seek(0) to read from start
File Doesn't ExistFileNotFoundErrorCreates new fileCreates new file
File ExistsNo error, allows read/write from startOverwrites existing fileNo overwrite, writing always appends to end

Chapter 4: Reading and Writing Data

Reading Data from a File

1. read([n])

Reads at most n bytes; if n is unspecified, reads the entire file. Returns the read bytes as a string.

2. readline()

Reads a line of input. Returns the read bytes as a string ending with a newline character. Returns blank string if no more bytes left.

3. readlines()

Reads all the lines of a text file. Returns them in the form of a list.

Writing Data to a File

write(string)

Takes a string and writes it in the file. If file doesn't exist, creates new file. If exists, writes data in the file.

writelines(list)

Writes a list of strings to the file. Does not automatically add newlines.

Appending Data

Appending means to add data at the end of a file using 'a' mode. If the file doesn't exist, it creates a new file. If the file exists, it appends the data at the end.

Chapter 5: Random Access in Files

File Positioning Functions

seek(offset, whence)

Moves the file pointer to a specific position in the file.

Parameters:

  • offset: Number of bytes to move
  • whence: Reference point (0=beginning, 1=current, 2=end)

tell()

Returns the current position of the file pointer.

Paths

Absolute Path

The full path from the topmost level of the directory structure.

/Users/username/Documents/file.txt

Relative Path

Path relative to the current working directory.

Chapter 6: Binary Files and Pickle

Binary Files

Most files in a computer system are binary files. They store data in the same format as held in memory, with no delimiters to separate lines. Generally considered more secure than text files.

Image Files

.png, .jpg, .gif, .bmp

Video Files

.mp4, .3gp, .mkv, .avi

Audio Files

.mp3, .wav, .mka, .aac

Other Files

.zip, .rar, .exe, .dll

Pickle Module

Pickling

Process of converting a Python object into a byte stream. This byte stream can be saved to a file.

Unpickling

Process of converting a byte stream back into the original Python object.

Key Methods: pickle.dump(obj, file) for writing,pickle.load(file) for reading

Chapter 7: CSV Files and Python csv Module

What is CSV?

CSV stands for Comma Separated Values. It is a simple file format used to store tabular data, such as a spreadsheet or database. By default, data fields are separated by commas. Other common delimiters include tab (\t), colon (:), and semicolon (;).

Advantages of CSV Files

  • Easier to create and edit
  • Overwrites existing data if file exists, allowing easy updates
  • Capable of storing large amounts of data efficiently

Python csv Module

The csv module provides objects for reading from and writing to CSV files.

Writing to CSV Files

csv.writer()

Returns a writer object configured to write to a CSV file.

writerow() and writerows()

writerow() writes one row, writerows() writes multiple rows.

Note: The newline='' argument ensures consistent handling of end-of-line characters across different operating systems.

Reading from CSV Files

csv.reader()

Returns a reader object which iterates over lines in the CSV file, interpreting data into lists.

Custom Delimiters

CSV files can use different delimiters (not just commas). You can specify a custom delimiter when reading or writing.

Chapter 8: Closing Files

close() Function

The close() function breaks the link of the file object and the file on the disk. After close(), no tasks can be performed on that file through the file object or file handle.

Key Takeaways

  • File Types: Text files (human-readable) vs Binary files (binary format)
  • Opening Files: Use with statement for automatic closure
  • File Modes: r, r+, w, w+, a, a+ (text) and rb, rb+, wb, wb+, ab, ab+ (binary)
  • Reading: read(), readline(), readlines() methods
  • Writing: write(), writelines() methods
  • File Positioning: seek() to move pointer, tell() to get current position
  • Binary Files: Use pickle module - pickle.dump() to write, pickle.load() to read
  • CSV Files: Use csv module - csv.writer() and csv.reader() with support for custom delimiters
Get in Touch