Opening Files
python
# Modes: 'r' read, 'w' write, 'a' append, 'r+' read+write
f = open("data.txt", "r")
content = f.read()
f.close()
# Better: using 'with'
with open("data.txt", "r") as f:
content = f.read()
print(content)Reading Files
python
with open("data.txt", "r") as f:
print(f.read()) # entire file
print(f.readline()) # one line
print(f.readlines()) # list of lines
for line in f:
print(line.strip())Writing Files
python
# Write (overwrites)
with open("output.txt", "w") as f:
f.write("Hello Knobly!\n")
f.write("Second line")
# Append
with open("output.txt", "a") as f:
f.write("\nAppended text")CSV Files
python
import csv
# Reading CSV
with open("data.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
print(row)
# Writing CSV
with open("out.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["Name", "Age"])
writer.writerow(["Knobly", 2])