File Handling Programs

Python programs that demonstrate reading, writing, appending, and processing text, CSV, JSON, and binary files.

20 program solutions

Back to Topics

Program Solutions

1

Read File

Read and print the contents of a text file using a context manager.

Beginner
View Solution
2

Write File

Write user input lines to a new text file.

Beginner
View Solution
3

Append to File

Append text to an existing file without overwriting the previous content.

Beginner
View Solution
4

Read CSV File

Read a CSV file and print each row using the csv module.

Beginner
View Solution
5

Write CSV File

Write a few sample rows to a CSV file.

Beginner
View Solution
6

JSON Read

Read a JSON file into a Python dictionary using the json module.

Beginner
View Solution
7

JSON Write

Write a Python dictionary as JSON into a file.

Beginner
View Solution
8

File Copy

Copy the contents of one file to another.

Beginner
View Solution
9

File Rename

Rename a file using the os module.

Beginner
View Solution
10

Read Binary File

Read bytes from a binary file and print their length.

Beginner
View Solution
11

Write Binary File

Create a small binary file from bytes.

Beginner
View Solution
12

Count Words in File

Count the number of words in a text file.

Beginner
View Solution
13

Count Lines in File

Count how many lines are present in a text file.

Beginner
View Solution
14

Count Characters in File

Count the total number of characters in a text file.

Beginner
View Solution
15

Search Substring in File

Search for a substring in a text file and report matching line numbers.

Beginner
View Solution
16

Iterate File Lines

Demonstrate safe iteration over lines in a file with a context manager.

Beginner
View Solution
17

File Metadata

Retrieve basic file metadata like size and modification time.

Intermediate
View Solution
18

File Compression

Compress a text file using the gzip module.

Intermediate
View Solution
19

File Decompression

Decompress a gzip file back to plain text.

Intermediate
View Solution
20

Delete File

Delete a file from disk using os.remove.

Beginner
View Solution