File Handling Programs
open, read, write, json, csv. Always use with. Always.
20 programs · open one, then the next
The matching chapter is Day 14: File Handling in the Python course. For a short check, try the Python quiz (300 questions). Every topic is listed on Python exercises.
Start with Read File- 01Read FileBeginnerRead and print the contents of a text file using a context manager.Open example →py# Program to read a text file and print its contents filename = input("Enter filename: ")
- 02Write FileBeginnerWrite user input lines to a new text file.Open example →py# Program to write lines to a text file filename = input("Enter filename to write: ") lines = []
- 03Append to FileBeginnerAppend text to an existing file without overwriting the previous content.Open example →py# Program to append text to a file filename = input("Enter filename to append to: ") text = input("Enter text to append: ")
- 04Read CSV FileBeginnerRead a CSV file and print each row using the csv module.Open example →py# Program to read a CSV file import csv
- 05Write CSV FileBeginnerWrite a few sample rows to a CSV file.Open example →py# Program to write to a CSV file import csv
- 06JSON ReadBeginnerRead a JSON file into a Python dictionary using the json module.Open example →py# Program to read JSON from a file import json
- 07JSON WriteBeginnerWrite a Python dictionary as JSON into a file.Open example →py# Program to write JSON to a file import json
- 08File CopyBeginnerCopy the contents of one file to another.Open example →py# Program to copy a text file src = input("Enter source filename: ") dst = input("Enter destination filename: ")
- 09File RenameBeginnerRename a file using the os module.Open example →py# Program to rename a file import os
- 10Read Binary FileBeginnerRead bytes from a binary file and print their length.Open example →py# Program to read a binary file filename = input("Enter binary filename: ")
- 11Write Binary FileBeginnerCreate a small binary file from bytes.Open example →py# Program to write a small binary file filename = input("Enter binary filename to write: ")
- 12Count Words in FileBeginnerCount the number of words in a text file.Open example →py# Program to count words in a file filename = input("Enter filename: ")
- 13Count Lines in FileBeginnerCount how many lines are present in a text file.Open example →py# Program to count lines in a file filename = input("Enter filename: ")
- 14Count Characters in FileBeginnerCount the total number of characters in a text file.Open example →py# Program to count characters in a file filename = input("Enter filename: ")
- 15Search Substring in FileBeginnerSearch for a substring in a text file and report matching line numbers.Open example →py# Program to search for a substring in a file filename = input("Enter filename: ") term = input("Enter search term: ")
- 16Iterate File LinesBeginnerfor line in f: under with open(...). File closes even if you break or explode.Open example →py# Program to iterate over file lines filename = input("Enter filename: ")
- 17File MetadataIntermediateRetrieve basic file metadata like size and modification time.Open example →py# Program to get file metadata import os import time
- 18File CompressionIntermediateCompress a text file using the gzip module.Open example →py# Program to compress a text file using gzip import gzip
- 19File DecompressionIntermediateDecompress a gzip file back to plain text.Open example →py# Program to decompress a gzip file import gzip
- 20Delete FileBeginnerDelete a file from disk using os.remove.Open example →py# Program to delete a file import os