####讀取文件####url
with open('goods_info.txt', 'r', encoding='utf-8') as f:指針
f.seek(0) # 注意指針位置blog
goods_info = eval(f.read()) # eval()將字符串str當成有效的表達式來求值並返回計算結果圖片
####內容替換####utf-8
with open('123.txt','a+',encoding='utf-8') as f:rem
f.seek(0)字符串
all = f.read()get
new_all = all.replace('二','一')博客
f.seek(0)requests
f.truncate()
f.write(new_all)
f.flush()
####多文件讀取####
import os
with open('123.txt',encoding='utf-8') as f,open('333.txt','w',encoding='utf-8') as f2:
for line in f:
new_line = line.replace('一','二')
f2.write(new_line)
os.remove('123.txt')#刪文件
os.rename('123.txt','333')#更名
###圖片讀寫#####
在博客上,上傳一張圖片,保存圖片的URL
import requests
url = 'https://images2017.cnblogs.com/blog/1297828/201801/1297828-20180111115848004-1702177202.jpg'
img = requests.get(url).content
f = open('123.jpg','wb')# bytes ,以二進制模式打開
f.write(img)