Opening files the right way
Use the with statement. It opens the file and guarantees it is closed afterwards, even if an error occurs:
with open("notes.txt", "w", encoding="utf-8") as f:
f.write("First line\n")
f.write("Second line\n")
with open("notes.txt", encoding="utf-8") as f: # mode "r" is default
content = f.read()
print(content)File modes: "r" read (default), "w" write (erases existing content!), "a" append, "x" create-only, add "b" for binary ("rb", "wb"). Always pass encoding="utf-8" for text so the file behaves the same on every OS.
Inside the Coding Python app files are created in the app's private storage, so these examples run unchanged on Android.
Reading line by line
with open("notes.txt", encoding="utf-8") as f:
for number, line in enumerate(f, start=1):
print(number, line.rstrip()) # strip the trailing newline
with open("notes.txt", encoding="utf-8") as f:
lines = f.readlines() # list of lines
print(len(lines), lines[0])Iterating the file object directly reads one line at a time and works for files far larger than memory. read() loads the whole thing at once.
Appending and handling missing files
with open("log.txt", "a", encoding="utf-8") as f:
f.write("app started\n")
try:
with open("missing.txt") as f:
print(f.read())
except FileNotFoundError:
print("No such file - creating a default")
with open("missing.txt", "w") as f:
f.write("default\n")pathlib: modern path handling
from pathlib import Path
p = Path("data") / "scores.txt" # joins with the right separator
p.parent.mkdir(exist_ok=True)
p.write_text("10\n20\n30\n")
print(p.exists(), p.suffix, p.stem, p.name)
total = sum(int(x) for x in p.read_text().split())
print("Total:", total)
for file in Path("data").glob("*.txt"):
print("found", file)
p.unlink() # deletePath.read_text() / write_text() cover the common cases in a single call and avoid the with block entirely.
JSON files
JSON is the universal format for config files and web APIs, and it maps directly onto Python dicts and lists:
import json
settings = {"theme": "dark", "font_size": 14, "recent": ["a.py", "b.py"]}
with open("settings.json", "w") as f:
json.dump(settings, f, indent=2)
with open("settings.json") as f:
loaded = json.load(f)
print(loaded["recent"][0], type(loaded))
text = json.dumps(settings) # to a string
print(text)
print(json.loads('{"ok": true, "n": null}'))CSV files
import csv
rows = [["name", "score"], ["Ada", 93], ["Linus", 88]]
with open("scores.csv", "w", newline="") as f:
csv.writer(f).writerows(rows)
with open("scores.csv", newline="") as f:
for row in csv.DictReader(f):
print(row["name"], int(row["score"]) + 1)Always pass newline="" when opening CSV files, otherwise Windows inserts blank lines. For serious data work you would move to pandas, but csv handles most scripts.
Next lesson: modules and the standard library, where these json, csv and pathlib imports come from.
Frequently asked questions
Why should I use with open() in Python?
The with statement closes the file automatically when the block ends, even if an exception happens. Forgetting to close files can lose data and leak resources.
What is the difference between w and a mode?
w truncates the file to empty before writing; a keeps existing content and adds to the end. Both create the file if it does not exist.
How do I read a file line by line in Python?
Loop over the file object: for line in f. Each iteration yields one line including its newline; call line.rstrip() to remove it.
Can I read and write files in the Coding Python Android app?
Yes. The app's Python runtime has its own private working directory, so open(), pathlib, json and csv all work exactly as they do on a computer.