Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

ProgramsPython

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
  1. 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 →
  2. 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 →
  3. 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 →
  4. 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 →
  5. 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 →
  6. 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 →
  7. 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 →
  8. 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 →
  9. 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 →
  10. 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 →
  11. 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 →
  12. 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 →
  13. 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 →
  14. 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 →
  15. 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 →
  16. 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 →
  17. 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 →
  18. 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 →
  19. 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 →
  20. 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 →