PNL Learn

Files and Data Formats

Read, write and transform files: text, CSV and JSON.

Real programs read their input from files and leave their results in files. You will open, read and write text files safely, choose the right mode, work with raw bytes and encodings, handle OSError and errno, and use the standard streams. Then you build paths with pathlib, parse and produce CSV the way spreadsheets expect, load and save JSON while handling malformed input and types JSON cannot store, and write your own context managers. The course ends with realistic data jobs: a sales summary, a two-file merge, a safe save, a rotating log, streaming a huge log and a cleaning pass that writes a rejects file.

IntermediatePython7 lessons74 exercisesAbout 6 h
Create a free account

What you will learn

  • Text and binary files
  • Encodings and errno
  • pathlib
  • CSV
  • JSON
  • Context managers
  • Data cleaning

Lessons

  1. 1

    Reading and Writing Text Files

    Open files with the right mode, let with close them, read line by line and rewind a handle you have used up.

    11 exercises

  2. 2

    Binary Files, Modes and I/O Errors

    Create files exclusively, read and write raw bytes, pick the right encoding, handle OSError and errno, and write to the standard streams.

    12 exercises

  3. 3

    Paths and the File System

    Build paths with pathlib, create folders, find files with glob and handle files that are missing.

    11 exercises

  4. 4

    CSV

    Read and write spreadsheet data with the csv module, convert its strings, survive quoted commas and never forget newline="".

    12 exercises

  5. 5

    JSON

    Convert between Python and JSON, keep dump and dumps apart, navigate nested data, handle malformed input and teach json new types.

    12 exercises

  6. 6

    Writing Your Own Context Managers

    Build your own with-statement helpers with __enter__ and __exit__ or @contextmanager, decide when errors propagate, and use suppress and redirect_stdout.

    8 exercises

  7. 7

    Putting It Together

    Build real data jobs: a CSV to JSON sales summary, a two-file merge, a safe save, a rotating log, streaming a huge log and a cleaning pass with a rejects file.

    8 exercises

How you practice

You practice in the browser and every exercise gives you feedback right away. This course uses these formats:

  • Code exercise: 25
  • Fix the bug: 18
  • Type the answer: 9
  • Predict the output: 8
  • Multiple choice: 4
  • Fill in the blank: 4
  • Select all that apply: 2
  • Match the pairs: 2
  • Reorder lines: 1
  • Spot the bug: 1

Aligned to

PCAP-31-03Python InstituteSource
  • Miscellaneous
    • 5.4Understand basic input/output terminology
    • 5.5Perform input/output operations
CS2023ACM / IEEE-CS / AAAISource
  • Software Development Fundamentals
    • SDF-FundamentalsFundamental Programming Concepts and Practices

Proto Node Labs is not affiliated with or endorsed by Python Institute and ACM / IEEE-CS / AAAI. Exam names are trademarks of their owners.