python os操作文件,复制文件,复制图片

271 阅读1分钟

有这样一种场景:

有一个文件夹,文件夹下面有很多子文件夹,子文件夹是按照日期来命名的,子文件夹里有很多文件,文件名是日期时间加一些盐,并且,这些日期是不和父文件夹的日期对应的
那么,你想把子文件夹和子文件夹里面的文件通过日期来对应,如何做呢。
import os
from PIL import Image
import time
allpath=r"你的文件夹的路径"


def eachFile(filepath):
    pathDir =  os.listdir(filepath)
    for allDir in pathDir:
        child = os.path.join('%s\%s' % (filepath, allDir))
        if not os.path.isfile(child):
            for dr in os.listdir(child):
                apppath=os.path.join('%s\%s' % (child, dr))
                if not os.path.isfile(apppath):
                    video_name = apppath.split("\\")[-1]
                    for drs in os.listdir(apppath):
                        try:
                            endpath = os.path.join('%s\%s\%s' % (allpath, str(drs[0:8]), video_name))
                            if not os.path.exists(endpath):
                                os.makedirs(endpath)
                            startimgpath = os.path.join('%s\%s' % (apppath, drs))
                            img = Image.open(startimgpath)
                            endmgpaths=os.path.join('%s\%s\%s\%s' % (allpath,str(drs[0:8]), video_name,drs))
                            img.save(endmgpaths)
                            print(endmgpaths)
                        except Exception as e:
                            print(e,'123123')
                        # pass
                        # print(e)
end=os.getcwd()
eachFile(end)