- r :仅读
- r+ :读写(可读可写,若文件不存在则报错,覆盖写)
- w :仅写
- w+:读写(可读可写,若文件不存在则创建)
- a :附加写 不可读
- a+ :附加读写 不覆盖写
txt 读
with open("data/....txt", 'r') as f:
data = f.read()
txt 写
with open('data/....txt', 'w+', encoding='utf-8', newline='') as f:
f.write([...])
csv 读
import csv
with open('data\....csv', 'r', encoding='utf-8') as file:
reader = csv.reader(file)
lows = [low for low in reader]
csv 写
import csv
with open('data/....csv', 'w+', encoding='utf-8', newline='') as file:
csv_writer = csv.writer(file)
csv_writer.writerow([...])