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 FileBeginnerRead and print the contents of a text file using a context manager.Open example →
  2. 02Write FileBeginnerWrite user input lines to a new text file.Open example →
  3. 03Append to FileBeginnerAppend text to an existing file without overwriting the previous content.Open example →
  4. 04Read CSV FileBeginnerRead a CSV file and print each row using the csv module.Open example →
  5. 05Write CSV FileBeginnerWrite a few sample rows to a CSV file.Open example →
  6. 06JSON ReadBeginnerRead a JSON file into a Python dictionary using the json module.Open example →
  7. 07JSON WriteBeginnerWrite a Python dictionary as JSON into a file.Open example →
  8. 08File CopyBeginnerCopy the contents of one file to another.Open example →
  9. 09File RenameBeginnerRename a file using the os module.Open example →
  10. 10Read Binary FileBeginnerRead bytes from a binary file and print their length.Open example →
  11. 11Write Binary FileBeginnerCreate a small binary file from bytes.Open example →
  12. 12Count Words in FileBeginnerCount the number of words in a text file.Open example →
  13. 13Count Lines in FileBeginnerCount how many lines are present in a text file.Open example →
  14. 14Count Characters in FileBeginnerCount the total number of characters in a text file.Open example →
  15. 15Search Substring in FileBeginnerSearch for a substring in a text file and report matching line numbers.Open example →
  16. 16Iterate File LinesBeginnerfor line in f: under with open(...). File closes even if you break or explode.Open example →
  17. 17File MetadataIntermediateRetrieve basic file metadata like size and modification time.Open example →
  18. 18File CompressionIntermediateCompress a text file using the gzip module.Open example →
  19. 19File DecompressionIntermediateDecompress a gzip file back to plain text.Open example →
  20. 20Delete FileBeginnerDelete a file from disk using os.remove.Open example →