Skip to contentSkip to navigation

Module 7

File Handling and Data Formats

Read and write files, manipulate modern paths with pathlib, and parse CSV and JSON data.

// Lessons

6

// Quiz questions

90

// Reading time

155 min

// Exam length

15 Q

// Coding exercises

10

  1. 1Text File I/O & Context ManagersOpen, read, write, and append disk files safely using the with statement and automatic resource cleanup. 25 min 15 questions
  2. 2Filesystem Navigation with PathlibConstruct cross-platform directory paths, inspect file metadata, and pattern-match with glob. 25 min 15 questions
  3. 3CSV Data Ingestion & ExportParse, filter, and write tabular comma-separated records using csv.reader and csv.DictReader. 25 min 15 questions
  4. 4JSON Serialization & API PayloadsEncode and decode JSON objects, handle file streams, and convert complex custom types. 25 min 15 questions
  5. 5Unicode Encodings & Raw Binary FilesManage bytes versus strings, encode UTF-8 text, handle binary byte buffers, and prevent decoding errors. 25 min 15 questions
  6. 6ETL Pipeline ArchitectureDesign clean data extraction, schema validation, record transformation, and loading pipelines. 30 min 15 questions

Coding practice: write it yourself

Real Python, real checks: write your answer, press Run checks, and keep going until it passes. (4 exercises)

All module exercises

Coding practice needs an account. Sign in or create a free ID to run these exercises.

Lab 1: Path stem extraction

Sign-in required

Construct a pathlib.Path("data/reports/q3_earnings.xlsx"). Extract and print its stem and suffix: Stem: q3_earnings, Suffix: .xlsx.

Coding Practice is Locked

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and earn XP.

Lab 2: CSV salary sum

Sign-in required

Parse CSV text "name,pay\nAli,500\nSara,700" using csv.DictReader and io.StringIO. Print: Total payroll: 1200.

Coding Practice is Locked

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and earn XP.

Lab 3: Serialize JSON

Sign-in required

Serialize {"status": "healthy", "uptime": 99.9} to a JSON string with indent=2 and print it.

Coding Practice is Locked

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and earn XP.

Lab 4: UTF-8 byte length

Sign-in required

Encode the string "Python 🐍" to UTF-8 bytes. Print: Byte length: 11.

Coding Practice is Locked

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and earn XP.

References & Further Reading

Related entries from PyLabs's own reference library.

  • open() (Open a file so you can read from it or write to it.)
  • read() (Read at most size characters or bytes from the file.)
  • write() (Write a string or bytes to the file stream.)