Working with Files & Data
Reading files, writing CSVs, parsing JSON, and calling APIs — how to get real-world data in and out of your Python programs.
The intern who automated 4 hours of work in 12 lines
In 2019, a marketing intern at a mid-size e-commerce company was given a daily task: download a CSV report from the analytics dashboard, open it in Excel, filter out rows where revenue was below $10, calculate the daily average, and email a summary to the team. It took about 4 hours every day — downloading, clicking, copying, pasting, formatting.
After learning basic Python, the intern wrote 12 lines of code that did the entire job in 3 seconds. Download the file, filter it, calculate the average, format the summary. The manager was so impressed that the intern was promoted within three months.
That intern did not use machine learning. Did not use AI. Just read a file, processed the data, and wrote the output. The most practically valuable Python skill is not fancy algorithms — it is moving data in and out of files and APIs.
Reading and writing text files
The simplest way to work with data is plain text files. Python's built-in open() function handles this.
# Writing to a file
with open("notes.txt", "w") as file:
file.write("Line 1: Hello from Python\n")
file.write("Line 2: This is a text file\n")
file.write("Line 3: Easy, right?\n")
# Reading a file
with open("notes.txt", "r") as file:
content = file.read()
print(content)
# Reading line by line
with open("notes.txt", "r") as file:
for line in file:
print(line.strip()) # strip() removes trailing newline
| Mode | What it does | Creates file? |
|---|---|---|
"r" | Read only | No — crashes if file missing |
"w" | Write (overwrites everything) | Yes |
"a" | Append (adds to end) | Yes |
"r+" | Read and write | No |
There Are No Dumb Questions
"What does 'w' mode do if the file already exists?"
It erases everything and starts fresh. This is the most dangerous file mode for beginners — you can accidentally delete hours of data with one
open("important.txt", "w"). If you want to add to a file without erasing, use"a"(append) mode. Always double-check your mode before running."What is
\n?"It is a "newline character" — it tells the computer "go to the next line." When you press Enter in a text editor, it inserts a
\nbehind the scenes. When reading files,strip()removes it from the end of each line.
CSV files — the spreadsheet of programming
CSV (Comma-Separated Values) is the most common data format in the world. Every spreadsheet app can export CSV. Every database can import it. It is just text with commas between values:
name,age,city,salary
Alice,30,London,75000
Bob,25,New York,68000
Charlie,35,Tokyo,82000
Python has a built-in csv module:
import csv
# Reading a CSV file
with open("employees.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(f"{row['name']} earns ${row['salary']}")
# Writing a CSV file
data = [
{"name": "Alice", "score": 95},
{"name": "Bob", "score": 87},
{"name": "Charlie", "score": 92},
]
with open("scores.csv", "w", newline="") as file:
writer = csv.DictWriter(file, fieldnames=["name", "score"])
writer.writeheader()
writer.writerows(data)
Analyze a CSV
25 XPJSON — the language of APIs
JSON (JavaScript Object Notation) is how data moves across the internet. When an app fetches weather data, user profiles, or stock prices, it arrives as JSON. And JSON looks almost identical to Python dictionaries:
{
"name": "Alice",
"age": 30,
"skills": ["Python", "SQL", "Excel"],
"address": {
"city": "London",
"country": "UK"
}
}
Python's json module converts between JSON strings and Python data:
import json
# Python dict → JSON string
data = {"name": "Alice", "age": 30, "skills": ["Python", "SQL"]}
json_string = json.dumps(data, indent=2)
print(json_string)
# JSON string → Python dict
json_text = '{"name": "Bob", "age": 25}'
parsed = json.loads(json_text)
print(parsed["name"]) # "Bob"
# Read JSON from a file
with open("data.json", "r") as file:
data = json.load(file)
# Write JSON to a file
with open("output.json", "w") as file:
json.dump(data, file, indent=2)
| Function | Direction | Source |
|---|---|---|
json.dumps() | Python → JSON string | Dictionary in memory |
json.loads() | JSON string → Python | String variable |
json.dump() | Python → JSON file | Writes to file |
json.load() | JSON file → Python | Reads from file |
Working with APIs — getting live data
An API (Application Programming Interface) is a URL that returns data instead of a web page. You send a request, and the server sends back JSON.
import urllib.request
import json
# Fetch data from a public API
url = "https://api.open-meteo.com/v1/forecast?latitude=40.71&longitude=-74.01¤t_weather=true"
with urllib.request.urlopen(url) as response:
data = json.loads(response.read())
weather = data["current_weather"]
print(f"Temperature: {weather['temperature']}C")
print(f"Wind speed: {weather['windspeed']} km/h")
This fetches live weather data for New York City using a free, no-signup API. No API key required.
Step 1 — Construct the URL with any required parameters (latitude, longitude, etc.)
Step 2 — Send the request with urllib.request.urlopen()
Step 3 — Read the response and parse the JSON with json.loads()
Step 4 — Access the data like a Python dictionary — because it IS one now
There Are No Dumb Questions
"Do I need the
requestslibrary? I see it in every tutorial."
requestsis a third-party library that makes API calls easier and more readable.urllibis built into Python — no installation needed. For learning,urllibis fine. For real projects, installrequests(we will cover this in Module 7). The concepts are identical."What if the API is down or the request fails?"
Your program will crash with a
URLError. In production code, you wrap API calls in atry/exceptblock to handle failures gracefully. For now, just know that network requests can fail and error handling is important.
Fetch Live Data
25 XPError handling — when things go wrong
Files can be missing. APIs can be down. Users can enter garbage. Error handling lets your program deal with problems gracefully instead of crashing.
# Without error handling — crashes on bad input
age = int(input("Enter your age: ")) # Crashes if user types "abc"
# With error handling — recovers gracefully
try:
age = int(input("Enter your age: "))
print(f"You are {age} years old")
except ValueError:
print("That is not a valid number!")
# Multiple except blocks
try:
with open("data.csv", "r") as file:
data = file.read()
value = int(data.split(",")[0])
except FileNotFoundError:
print("File not found — check the filename")
except ValueError:
print("File contains non-numeric data")
except Exception as e:
print(f"Unexpected error: {e}")
The pattern: try the risky thing. If it fails, except catches the specific error and runs alternative code.
Build a Data Pipeline
50 XPKey takeaways
with open()is the safe way to read and write files — it automatically closes the file, even on errors"w"mode erases everything — use"a"(append) if you want to add to an existing file- CSV values are always strings — convert to
int()orfloat()before doing math json.dumps()/json.loads()work with strings;json.dump()/json.load()work with files — the "s" = "string"- APIs return JSON — fetch with
urllib, parse withjson.loads(), access like a dictionary try/excepthandles errors gracefully — always wrap file and network operations- This is the most practical Python skill — most real-world automation is reading, processing, and writing data
Knowledge Check
1.What is the danger of opening a file with `open('data.txt', 'w')`?
2.When reading a CSV file with csv.DictReader, what data type are all values?
3.What is the difference between `json.dump()` and `json.dumps()`?
4.What Python construct should you use to handle a FileNotFoundError gracefully?