Python + AI/ML · Tutorial 7 of 7
Files and APIs in Python: reading, writing and requests
By the FUTEAC Team · 20 November 2026 · 14 min read · Beginner
In short
Python reads and writes files with open(), and it talks to web APIs with the requests library. You read a small expenses CSV, total it by category, save the result as JSON, then fetch a real user record from a public API. Every step runs as written, and the two most common beginner errors are shown with their fixes.
- Files are how programs remember things. A CSV file holds your data after the program ends, and reading it back is the first step of almost every data job.
open()withwithis the only pattern you need for files. It opens the file, hands it to you, and closes it for you, even when something goes wrong halfway through.- CSV rows arrive as text, even the numbers. The
csvmodule hands you strings, so you convert amounts withfloat()before you add them, or Python refuses. - JSON is the format APIs speak. Python's
jsonmodule turns a JSON file or an API reply into a dictionary you already know how to use. requests.get()fetches a web page or an API reply in one line. Checkstatus_codebefore you call.json(), because a failed request does not return JSON.- This tutorial feeds the Python Programming Foundations course (/course/python-programming-foundations) and the Files & APIs in Python (/skill/files-and-apis-python) skill.
Before you start
Why files and APIs belong in the same tutorial
A program that cannot read a file can only work with data you type in by hand. A program that cannot call an API can only work with data already on your computer. Files and APIs are the two doors data comes through, and the pattern for both is the same: open a source, read structured data out of it, and do something with the result.
Think of a file like a filing cabinet drawer and an API like a counter where you place an order. With the drawer, you pull out a folder and read it. With the counter, you ask for something by name and get a tray back. In both cases you get a result you can hold in your hands, a list of rows or a dictionary, and the rest of your program works the same way from there.
The worked example in this tutorial is a small stationery shop. Its expenses live in expenses.csv, five rows. You read the file, total the spending by item, save the summary as JSON, then fetch one customer record from a public test API. The shop is small on purpose. The pattern scales to a file with five million rows and an API with a thousand endpoints, and nothing in the pattern changes.
---
Reading a text file: `open()` and `with`
Start with the simplest file there is, a plain text note. Create note.txt with one line:
Buy more notebooks before Friday.Read it back in Python:
with open("note.txt", encoding="utf-8") as f:
text = f.read()
print(text)Buy more notebooks before Friday.Two details matter. First, with open(...) as f: opens the file and closes it for you when the block ends. Close it yourself and you will forget one day, usually on the day it matters. Second, encoding="utf-8" tells Python how to read the characters. Skip it and Python guesses from your computer's settings, which is fine until a colleague on another computer opens the same file and gets different characters.
f.read() returns the whole file as one string. For a file with lines you want one at a time, use f.readlines() or loop straight over the file object:
with open("note.txt", encoding="utf-8") as f:
for line in f:
print(line.strip())line.strip() removes the newline character at the end of each line. Without it, print adds its own newline and you get a blank line after every line of the file. Small detail, and it confuses everyone once.
---
Writing a file, and why `with` matters
Writing uses the same call with a different mode letter, "w" for write:
with open("shopping.txt", "w", encoding="utf-8") as f:
f.write("notebooks\n")
f.write("pens\n")The file shopping.txt now holds two lines. Open it with "w" again and Python empties it first, then writes. That is the behaviour that bites beginners: "w" means "start from empty", not "add to the end". To add to a file that already exists, use "a" for append:
with open("shopping.txt", "a", encoding="utf-8") as f:
f.write("printer paper\n")Now the file holds all three lines. Our opinion: always use with for files, and always pass the encoding. Both cost you nothing and both prevent a class of error that is boring to debug and easy to avoid.
---
CSV files: rows that arrive as text
Real data rarely arrives as plain text. It arrives as CSV, comma-separated values, one row per line, with a header row naming the columns. Here is the shop's expenses file, expenses.csv:
date,item,amount
2026-11-10,notebooks,450
2026-11-11,pens,180
2026-11-12,notebooks,300
2026-11-13,printer paper,520
2026-11-14,pens,120Read it with the csv module, which is part of Python's standard library, so nothing needs installing:
import csv
with open("expenses.csv", newline="", encoding="utf-8") as f:
rows = list(csv.DictReader(f))
print(rows[0])
print(len(rows), "rows"){'date': '2026-11-10', 'item': 'notebooks', 'amount': '450'}
5 rowsDictReader turns each row into a dictionary keyed by the header names, so row["item"] reads better than row[1] and survives a column being reordered. The newline="" argument stops Python translating line endings inside the file, which is what the csv documentation asks for. On Linux it changes nothing visible, and on Windows it prevents blank rows appearing between every real row.
Notice the first line of output. The amount is '450', in quotes, a string. The CSV file holds text, and the csv module hands you text. That fact is the whole of the next section's wrong turn, so keep it in mind.
Now total the spending by item:
totals = {}
for row in rows:
totals[row["item"]] = totals.get(row["item"], 0) + float(row["amount"])
print(totals)
print("Grand total:", sum(totals.values())){'notebooks': 750.0, 'pens': 300.0, 'printer paper': 520.0}
Grand total: 1570.0float(row["amount"]) is the line that does the real work. It converts the string '450' into the number 450.0, so the addition works. totals.get(row["item"], 0) returns the running total for that item, or 0 the first time the item appears, which is how the dictionary builds itself up without an if check.
---
JSON: the format APIs speak
JSON looks like a Python dictionary written out as text, and that is close to what it is. Curly braces, keys in double quotes, values that are strings, numbers, lists or more dictionaries. Python's json module converts between JSON text and Python values in both directions: json.load() reads a file into a dictionary, json.dump() writes a dictionary out to a file.
Save the expense totals as JSON:
import json
summary = {"items": totals, "grand_total": sum(totals.values()), "rows": len(rows)}
with open("summary.json", "w", encoding="utf-8") as f:
json.dump(summary, f, indent=2)Read it back to check:
with open("summary.json", encoding="utf-8") as f:
print(f.read()){
"items": {
"notebooks": 750.0,
"pens": 300.0,
"printer paper": 520.0
},
"grand_total": 1570.0,
"rows": 5
}indent=2 makes the file readable by a human. Without it, json.dump writes the whole dictionary on one line, which is valid JSON and miserable to read. JSON is a storage format and a transfer format at once: the file you just wrote could be sent to another program, in any language, and that program could read it without knowing anything about Python.
---
Your first API call with `requests`
An API is a web address that returns data instead of a web page. The public test API at jsonplaceholder.typicode.com returns fake user records as JSON, which makes it the standard first API for learners: it is free, needs no key, and always answers.
Install the requests library first. It is not part of Python's standard library, and it is the one library every Python tutorial reaches for, because it turns an API call into two readable lines:
pip install requestsOur opinion: use requests instead of Python's built-in urllib for your first API work. urllib can do the same job, but it takes more code and hides errors behind longer tracebacks. requests is what working Python developers actually use, and the code you write with it reads like the thing it does.
Now fetch one user:
import requests
response = requests.get("https://jsonplaceholder.typicode.com/users/1", timeout=10)
print("Status code:", response.status_code)
user = response.json()
print(user["name"], "-", user["address"]["city"], "-", user["email"])Status code: 200
Leanne Graham - Gwenborough - Sincere@april.bizThree things to notice. First, requests.get() returns a response object, not the data itself. Second, status_code 200 means the request worked. Anything starting with 4 or 5 means it did not, and you should check it before going further. Third, response.json() converts the JSON reply into a dictionary, and from there it is ordinary Python: user["name"] and user["address"]["city"] work exactly like the CSV dictionaries from the last section. The API reply is nested, a dictionary inside a dictionary, and you read it one key at a time.
The timeout=10 argument stops your program waiting forever if the server never answers. Ten seconds is a sensible default for a learner's script. Leave it out and a dead server can freeze your program with no error and no message.
---
The wrong turn: calling `.json()` on a page that is not JSON
Two mistakes hide in the sections above, and both are worth meeting here, where the fix is one line each, rather than in a real project at midnight.
The first is the CSV string trap. Add the amounts without converting them:
total = 0
for row in rows:
total = total + row["amount"]
print(total)Python answers:
Traceback (most recent call last):
File "totals.py", line 8, in <module>
total = total + row["amount"]
TypeError: unsupported operand type(s) for +: 'int' and 'str'The message is exact: you asked Python to add an integer and a string, and it cannot. The CSV module gave you '450', a string that looks like a number, and Python will not guess that you meant the number. The fix is the float() call from the earlier section. This error is a gift, because Python fails loudly at the first row instead of silently adding strings together. The same mistake in a spreadsheet would have produced a wrong total with no warning at all.
The second mistake is calling .json() on a reply that is not JSON. Fetch an ordinary web page and treat it like an API:
response = requests.get("https://example.com", timeout=10)
print("Status code:", response.status_code)
data = response.json()Python answers:
Status code: 200
Traceback (most recent call last):
...
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)The status code is 200, so the request worked, but the page is HTML, not JSON, and .json() fails on the very first character. The status code tells you the request succeeded. It tells you nothing about what came back. The fix is to check the status code first, then call .json() only when you know the reply is JSON:
response = requests.get("https://jsonplaceholder.typicode.com/users/1", timeout=10)
response.raise_for_status()
user = response.json()raise_for_status() turns a 404 or a 500 into a loud error with the status code in it, instead of letting your program carry on with a reply it cannot use. Put it after every requests.get() you write. It is one line, and it is the difference between an error message that names the problem and an hour of wondering why your dictionary is empty.
---
Putting it together: one script, end to end
Here is the whole worked example as one file, expenses_report.py. It reads the CSV, totals it, writes the JSON summary, then fetches the user record, in the order a real script would:
import csv
import json
import requests
with open("expenses.csv", newline="", encoding="utf-8") as f:
rows = list(csv.DictReader(f))
totals = {}
for row in rows:
totals[row["item"]] = totals.get(row["item"], 0) + float(row["amount"])
summary = {"items": totals, "grand_total": sum(totals.values()), "rows": len(rows)}
with open("summary.json", "w", encoding="utf-8") as f:
json.dump(summary, f, indent=2)
response = requests.get("https://jsonplaceholder.typicode.com/users/1", timeout=10)
response.raise_for_status()
user = response.json()
print("Rows read:", len(rows))
print("Totals:", totals)
print("Grand total:", summary["grand_total"])
print("Customer:", user["name"], "-", user["address"]["city"])Run it with python3 expenses_report.py:
Rows read: 5
Totals: {'notebooks': 750.0, 'pens': 300.0, 'printer paper': 520.0}
Grand total: 1570.0
Customer: Leanne Graham - GwenboroughTwo sources, one program. The CSV came from a file on your computer and the user record came from a server on the internet, and your code treated both the same way once they were dictionaries. That is the skill this tutorial teaches: get data in, whatever door it comes through, and turn it into values your program can work with.
---
Try it yourself
Extend expenses_report.py in three steps: 1. Add a second CSV file, sales.csv, with the same columns, date, item, amount, and five rows of your own numbers. Read it the same way and print the total sales. Then print profit, sales minus the expenses total, as one number. 2. Change the API call to fetch user 2 instead of user 1, at https://jsonplaceholder.typicode.com/users/2, and print the user's name, company name and city. The company name sits at user["company"]["name"], one more level down. 3. Fetch all ten users at https://jsonplaceholder.typicode.com/users and print each user's name and city on one line each. The reply is a list of dictionaries, so loop over it the way you looped over the CSV rows. If step 3 works, you have done the core move of API programming: one request returns a list, and your loop turns it into ten lines of output. Most real API work is that move, repeated with different addresses.
Continue in the free course: Python Programming Foundations →
Common mistakes
- Adding CSV values without converting them. The error is
TypeError: unsupported operand type(s) for +: 'int' and 'str'. Wrap the value infloat()(orint()for whole numbers) before you add it. - Opening a file that is not where Python is looking. The error is
FileNotFoundError: [Errno 2] No such file or directory: 'expenses.csv'. Python looks in the folder you ran it from, not the folder the script lives in. Run the script from the folder holding the file, or pass the full path. - Calling
.json()on a reply that is not JSON. The error isrequests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0). Checkstatus_codeand callraise_for_status()first, and only call.json()when the reply is JSON. - Forgetting
timeouton a request. A server that never answers freezes your program with no error. Always passtimeout=10(or similar) torequests.get(). - Using
"w"when you meant"a". Opening a file with"w"empties it first. If your earlier data vanished, you opened with"w"instead of"a"for append. Check the mode letter before you run the script again. - Writing JSON to a file opened in binary mode.
json.dump()writes text, so dumping to a file opened with"wb"raisesTypeError: a bytes-like object is required, not 'str'. Open JSON files in text mode and passindent=2so the file stays readable.
Frequently asked questions
Do I need requests, or can I use Python's built-in tools?
You can use urllib, which ships with Python, and it will fetch the same data. We use requests because the code is shorter, the errors are clearer, and it is the library working Python developers reach for first. Install it once with pip install requests and it stays installed.
What is the difference between json.load() and json.loads()?
json.load() reads JSON from an open file object. json.loads() reads JSON from a string. The s stands for string. response.json() is a third way: it reads the JSON straight from a requests response, so you rarely need either of the other two when you are working with APIs.
Why does csv.DictReader need newline=""?
Python normally translates line endings when it opens a text file, and the csv module does its own line handling on top of that. The two translations clash, and on Windows the clash shows up as a blank row between every real row. Passing newline="" turns Python's translation off and lets the csv module do the job alone.
What should I do when an API needs a key?
Keep the key out of your code. Read it from an environment variable with os.environ["API_KEY"] and pass it in a header, usually {"Authorization": "Bearer " + key}. Never paste a real key into a script you share or upload, because anyone who reads the script can use the key.
Can I write a CSV file as well as read one?
Yes, with csv.DictWriter. Open the file with "w" and newline="", pass the column names as fieldnames, call writer.writeheader() once, then writer.writerow() for each row. The same string rule applies in reverse: DictWriter writes whatever you give it, so pass numbers and it will convert them to text for you.
Follow the Python + AI/ML Beginner Roadmap
Learn Python from zero and take your first steps into AI and machine learning. Free, step by step, at your pace.
View the Roadmap