Skip to course content
Free course

Python Foundations / Module 7 / Files as Inputs and Outputs

Module 7 lesson

Files as Inputs and Outputs

Unit ID: M07-U00 Estimated active time: 18-25 minutes

A file workflow has roles

Consider a conversion task:

  • source_courses.csv: supplied evidence that must not change;
  • Python objects in memory: working records used during validation;
  • accepted_courses.json: output containing valid records;
  • rejected_courses.json: output containing rejected rows and reasons;
  • transformation_log.json: record of what happened.

Naming these roles before coding prevents accidental overwrite.

Files are not Python objects until parsed

A CSV file is stored text. Reading it does not automatically create correct integers or booleans. A CSV parser produces text fields, which your program must validate and convert.

A JSON parser can create dictionaries, lists, numbers, strings, booleans, and None, but those values still need task-specific validation.

Define the contract

For the Module 7 workflow:

  • source file is read-only evidence;
  • output files are written to a separate folder;
  • accepted records contain code, title, integer hours, and status;
  • rejected records keep source row number, safe ID, and reasons;
  • output can be regenerated from the source and documented code.

Do not use personal data

Use only the supplied fictional records. File exercises can accidentally persist information after the notebook closes, so never substitute private, employer, client, health, financial, authentication, or security data.

Inspect before transforming

Before code changes a value, inspect:

  • file name and location;
  • file size;
  • expected encoding;
  • header or top-level JSON shape;
  • row count;
  • required fields; and
  • whether output already exists.

Practice

For a fictional CSV-to-JSON task, write down the source, working objects, accepted output, rejected output, and transformation log. State which files may be replaced during a rerun and which must never be overwritten.

Takeaway

A reliable file workflow names source, working state, outputs, and evidence. Next, we will build portable paths without hard-coding one computer's folder layout.