NetAuto Academy

Python for Network Engineers

Working with Files & Structured Data

Beginner12 min read

What you'll learn

  • Why automation scripts read from and write to files, instead of only printing to the screen
  • How to read a text file, line by line, in Python
  • How to write output to a file, so a report survives after the script finishes
  • What CSV, JSON, and YAML are, at a conceptual level
  • That this lesson is a bridge — the dedicated 'Networking Data & Formats' module goes much deeper
🎓 Teach This Lesson

Listen to a guided explanation of this lesson.

Spoken narration isn't supported in this browser.

Why automation needs files, not just print()

Every code example so far has used print() to show a result — fine for learning, but not how real automation actually works. A script that checks 500 switches overnight needs its device list to come from somewhere before it starts, and its report to go somewhere after it finishes, because nobody's watching the screen at 2 a.m. That "somewhere" is almost always a file.

  1. Input filee.g. devices.csv — the inventory to work through
  2. Python scriptreads the input
  3. Process devicesthe loop, condition, and function logic from this module
  4. Output reporte.g. report.txt — written for someone to read later

Reading a file

read_devices.py
with open("devices.txt") as f:
  for line in f:
      print(line.strip())

open("devices.txt") opens the file. 'with' ensures it gets closed automatically when the block finishes, even if something goes wrong partway through. Looping over f gives you one line at a time; .strip() removes the trailing newline character each line comes with.

If devices.txt contains one hostname per line — SW1, then SW2, then SW3 — this script prints each one, exactly like looping over a list did in Lesson 4. That's not a coincidence: reading a file line by line and looping over a list are the same kind of repetition, just with the data coming from a file on disk instead of typed directly into the script.

Quick checkConcept check
Why does an automation script typically read its device list from a file instead of writing the list directly into the code?

Writing a report

write_report.py
with open("report.txt", "w") as f:
  f.write("SW1: synced\n")
  f.write("SW2: out of sync\n")

The "w" mode opens (or creates) the file for writing — anything already in report.txt is replaced. f.write() adds text to the file exactly as given; unlike print(), it doesn't add a line break automatically, which is why " " is written explicitly at the end of each line.

After this runs, report.txt exists on disk with two lines in it — a record that outlives the script itself, exactly what "Network Automation Fundamentals" meant by reporting being something a person can come back to and act on, not just a message that flashes by on a screen once.

Quick checkPredict output
After running the write_report.py code above, what does report.txt contain?

CSV, JSON, and YAML — a first, conceptual look

Real inventories and reports usually aren't plain, unstructured text — they use one of a few common structured formats. This module only needs you to recognize them; the dedicated Networking Data & Formats module (later in this curriculum) is where you'll actually parse and generate them properly.

CSV (comma-separated values) is a plain-text table — each line is a row, commas separate the columns:

hostname,ip,platform
SW1,10.10.10.1,cisco_ios
SW2,10.10.10.2,cisco_ios

That's devices.csv — a spreadsheet, saved as text. Each row maps naturally onto one of the device dictionaries from Lesson 3.

JSON writes structured data — the same lists and dictionaries you've been using all module — as text, almost exactly like Python's own syntax:

{
    "hostname": "SW1",
    "ip": "10.10.10.1",
    "platform": "cisco_ios"
}

That's what inventory.json looks like. If it looks nearly identical to a Python dictionary, that's because it is — JSON was designed to map directly onto exactly this kind of structured data.

YAML represents the same kind of structured data with indentation instead of braces and brackets, which is why it's popular for configuration files people edit by hand:

hostname: SW1
ip: 10.10.10.1
platform: cisco_ios

Three different ways of writing down the same idea — a device's hostname, IP, and platform — for three different situations: CSV for simple tabular lists, JSON for data passed between programs, YAML for configuration a person edits directly.

Quick checkConcept check
What do CSV, JSON, and YAML have in common, based on this lesson?

Why this lesson stops here

This is deliberately a light touch — recognizing that these formats exist and roughly what each is for, not writing code that parses them. Going deeper here would duplicate the dedicated "Networking Data & Formats" module later in this curriculum, which is exactly where CSV, JSON, and YAML each get the depth they deserve.

Quick checkScenario
A script needs to read a list of 200 switches from a file before it starts, and write a compliance report to a file when it finishes. What are those two files, in the terms this lesson used?

Key takeaways

  • Real automation scripts read their device list from a file and write their results to a file, rather than relying on print() and a device list hardcoded into the code.
  • open("file") with 'with' reads a file safely; looping over it gives one line at a time, same shape as looping over a list.
  • open("file", "w") together with .write() creates or overwrites a file — a report that outlives the script run itself.
  • CSV, JSON, and YAML are three different text formats for the same kind of structured data you've already been using as lists and dictionaries.
  • This lesson is a bridge, not the destination — the 'Networking Data & Formats' module goes much deeper into actually parsing and generating each of these formats.