File Handling Programs
Python programs that demonstrate reading, writing, appending, and processing text, CSV, JSON, and binary files.
20 program solutions
Program Solutions
Read File
Read and print the contents of a text file using a context manager.
Write File
Write user input lines to a new text file.
Append to File
Append text to an existing file without overwriting the previous content.
Read CSV File
Read a CSV file and print each row using the csv module.
Write CSV File
Write a few sample rows to a CSV file.
JSON Read
Read a JSON file into a Python dictionary using the json module.
JSON Write
Write a Python dictionary as JSON into a file.
File Copy
Copy the contents of one file to another.
File Rename
Rename a file using the os module.
Read Binary File
Read bytes from a binary file and print their length.
Write Binary File
Create a small binary file from bytes.
Count Words in File
Count the number of words in a text file.
Count Lines in File
Count how many lines are present in a text file.
Count Characters in File
Count the total number of characters in a text file.
Search Substring in File
Search for a substring in a text file and report matching line numbers.
Iterate File Lines
Demonstrate safe iteration over lines in a file with a context manager.
File Metadata
Retrieve basic file metadata like size and modification time.
File Compression
Compress a text file using the gzip module.
File Decompression
Decompress a gzip file back to plain text.
Delete File
Delete a file from disk using os.remove.