Stop 06 · Programming Fundamentals for AutomationTopics · 6/10

File Handling

Part of the QA Testing Roadmap

In short

Test data lives in files: a CSV from the business team, a JSON config, a results sheet your manager wants. Reading and writing files turns your script into a real tool.

Reading test data from files (CSV, JSON, Excel) and writing test output/reports to files — a common need in data-driven testing and custom reporting.

The purple blob sits beside two steel milk cans, writing in a small notebook with an older one open nearby.

Think of it like this

The doodhwala's notebook

Every morning the milkman reads last month's notebook to know who takes how many litres. After delivery he writes today's entry. Your script does the same: read test inputs from a file, run, then write results into another file.

Watch it happen

Data in, results out

users.csv
Read rows
Run test
Write row
results.csv

1/5The input file holds one test case per line: email, password, expected result.

The big ideas

What you need to know

Use with open()

with open(...) closes the file for you, even if something fails halfway.

Pick the mode

'r' reads, 'w' overwrites, 'a' adds to the end. 'w' on the wrong file wipes it.

CSV and JSON

CSV suits tables of test cases. JSON suits nested data like API payloads and configs.

Paths matter

Keep data files in a known folder like testdata/ and build paths from your project root.

See it in code

Walk through the code

python
1import csv2 3with open("testdata/users.csv", newline="") as infile:4    cases = list(csv.DictReader(infile))5 6with open("results.csv", "w", newline="") as outfile:7    writer = csv.writer(outfile)8    writer.writerow(["email", "expected", "status"])9    for case in cases:10        status = "PASS"  # replace with your real check11        writer.writerow([case["email"], case["expected"], status])12 13print(len(cases), "rows written to results.csv")

1/3DictReader reads the header row and gives you each line as a dictionary.

Read every row from a CSV, then write one result row per case. The file closes itself after each with block.

Save yourself the pain

Common mistakes

Oops

Hard-coding C:/Users/me/Desktop/data.csv in the script.

Do this instead

Use a relative path inside the project, so it works on your teammate's laptop and in CI.

Oops

Opening the input file with 'w' by accident and wiping your test data.

Do this instead

Double-check the mode. Read with 'r' (the default), write results to a separate file.

Your mission

CSV in, CSV out

~30 min

Quick check

You want to add new results to the end of yesterday's log file. Which mode do you use?

Go deeper

  1. 1Write a script that reads test data from a CSV file and uses it to drive a test loop.
  2. 2Practice writing test results to an output file for later review.
  3. 3Learn to handle a missing or malformed file gracefully rather than crashing.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge