提示:先写(写的过程会自动创建文件)、再追加、再读。
f = "songs.txt"
# 直接写入
with open(f,"w") as file: #”w"代表着每次运行都覆盖内容
file.write('难念的经')
# 追加内容
with open(f,"a") as file: #只需要将之前的”w"改为“a"即可,代表追加内容
file.write('let me down slowly')
# 读取内容
with open(f,"r") as file: #只需要将之前的”w"改为“r"即可,代表读取内容
for i in file:
print(str(i) + "d" + " "+"\n")