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 FileBeginnerOpen UTF-8 text, read it, and close it with Python with. Beginners handle a missing path while learning which decoding and permission errors remain uncaught.Open example →py# Program to read a text file and print its contents filename = input("Enter filename: ")
- 02Write FileBeginnerCollect Python text until a blank response, then write a UTF-8 file. Beginners place newlines deliberately and see why w mode can erase an existing document.Open example →py# Program to write lines to a text file filename = input("Enter filename to write: ") lines = []
- 03Append to FileBeginnerKeep old Python file contents while appending text in a mode. Beginners trace the inserted newline and predict blank lines for new or already-terminated files.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 Python CSV rows without breaking quoted commas. A beginner csv.reader example explains string fields, explicit headers, newline handling, and missing files.Open example →py# Program to read a CSV file import csv
- 05Write CSV FileBeginnerWrite Python table rows with csv.writer rather than hand-built commas. Beginners include a header, let the library quote fields, and understand overwrite mode.Open example →py# Program to write to a CSV file import csv
- 06JSON ReadBeginnerLoad a Python value from JSON and understand the changed spelling of true. Beginners separate missing paths, invalid JSON syntax, and a wrong data shape.Open example →py# Program to read JSON from a file import json
- 07JSON WriteBeginnerSerialize Python data to JSON in a UTF-8 file. Beginners distinguish dump from printing and recognize unsupported types before treating output as a backup.Open example →py# Program to write JSON to a file import json
- 08File CopyBeginnerCopy Python UTF-8 text a line at a time with two managed streams. Beginners preserve line breaks and learn why the source and destination must be different files.Open example →py# Program to copy a text file src = input("Enter source filename: ") dst = input("Enter destination filename: ")
- 09File RenameBeginnerAsk the operating system to rename a Python file path. New programmers distinguish copying from renaming and check platform-dependent risks at an existing target.Open example →py# Program to rename a file import os
- 10Read Binary FileBeginnerRead Python file bytes without guessing a text encoding. A beginner rb example counts byte length and explains why an unrestricted read can use too much memory.Open example →py# Program to read a binary file filename = input("Enter binary filename: ")
- 11Write Binary FileBeginnerStore five raw Python byte values with wb mode, not their printed digits. Beginners check the 0–255 range and understand truncation and the absence of encoding.Open example →py# Program to write a small binary file filename = input("Enter binary filename to write: ")
- 12Count Words in FileBeginnerApply a clear whitespace-token rule to Python file contents. Beginners read UTF-8, count with split and len, and recognize whole-file memory and language limits.Open example →py# Program to count words in a file filename = input("Enter filename: ")
- 13Count Lines in FileBeginnerCount Python file lines without keeping them all in a list. Beginners include blank lines and a final unterminated line while giving an empty file a zero count.Open example →py# Program to count lines in a file filename = input("Enter filename: ")
- 14Count Characters in FileBeginnerCount decoded Python text characters, including whitespace. A beginner file exercise explains why len can differ from byte size and from visible-symbol counts.Open example →py# Program to count characters in a file filename = input("Enter filename: ")
- 15Search Substring in FileBeginnerFind matching Python file lines and print their numbers. Beginners stream with enumerate, search case-sensitively, and inspect empty terms and stripped output.Open example →py# Program to search for a substring in a file filename = input("Enter filename: ") term = input("Enter search term: ")
- 16Iterate File LinesBeginnerNumber Python file lines as they are read instead of loading everything. A beginner streaming pattern explains enumerate and the indentation removed by strip.Open example →py# Program to iterate over file lines filename = input("Enter filename: ")
- 17File MetadataIntermediateInspect Python file byte size and modification time without reading contents. Beginners use os.stat and interpret time.ctime as a local-time display, not UTC.Open example →py# Program to get file metadata import os import time
- 18File CompressionIntermediateFeed Python file bytes into a gzip writer and close both streams safely. Beginners trace rb and gzip wb while learning why compression need not shrink a file.Open example →py# Program to compress a text file using gzip import gzip
- 19File DecompressionIntermediateRestore Python gzip data into bytes with a managed reader and writer. Beginners recognize corrupt input, destination overwrites, and large expansion risks.Open example →py# Program to decompress a gzip file import gzip
- 20Delete FileBeginnerRemove a disposable Python file path with os.remove. Beginners learn why an existence check cannot guarantee success and why this is not a recycle-bin action.Open example →py# Program to delete a file import os