基本文件操作


copy图片
import shutil
shutil.copy(srcimgpath,targetimgpath)

srcimg = Image.open(srcimgpath)
srcimg .save(targetimgpath, "JPEG")

删除文件
os.remove(srcimgpath)


打开文件并写入到redis当中,注意打开后要关闭,要不然无法对之后的文件进行操作
打开图片返回的是二进制编码,所以要转换为base64编码
from redis import Redis
import base64
redi = Redis()
with  open(photo,'rb') as img:
redi.set(photo, base64.b64encode(img.read()))


遍历文件夹
  1. for dirpath,dirnames,filenames in os.walk(startdir):#dirpath为父目录,dirnames为所有文件夹的名字,filenames为所有文件的名字
  2. for file in filenames:
  3. if os.path.splitext(file)[1] == '.xlsx':#还有菏泽杨file.split(".")[1]=='.xlsx'也可以
判断路径是否存在
os.path.exists(srcimgpath):
创建文件夹
os.makedirs(srcimgpath)





原文地址:https://www.cnblogs.com/wuqingzangyue/p/5749848.html