0
点赞
收藏
分享

微信扫一扫

python 实现多进程拷贝文件夹嵌套文件

汤姆torn 2022-01-20 阅读 154

1.面对文件夹嵌套很多的文件需要用到递归搜索

2.多进程复制,提高效率。我需要的文件很大有3个T

直接附代码:供大家参考

import os, multiprocessing
q = None
def getFilesCount(dirName,newDirName):
    if dirName == None or not os.path.isdir(dirName):
        return
    files = os.listdir(dirName)
    print(files)
    for f in files:
        p = os.path.join(dirName, f)
        pp = os.path.join(newDirName, f)
        if os.path.isdir(p):
            os.mkdir(pp)
            getFilesCount(p,pp)
        else:
            if q.full():
                print("缓存已满")
            else:
                q.put(p)
def copyFile(destination, file, oldDir):
    if destination == None or file == None:
        return
    if (not os.path.isdir(destination)) or (not os.path.exists(file)):
        return
    subDir = file[file.find(oldDir) + len(oldDir) + 1:file.rfind(os.sep)]
    newDir = destination + os.sep + subDir
    if not os.path.exists(newDir):
        os.mkdir(newDir)
    fileName = file[file.rfind(os.sep) + 1:]

    newFileName = newDir + os.sep + fileName
    newFile = open(newFileName, "wb")
    newFile.write(open(file, "rb").read())
    newFile.close()
def main():
    multiprocessing.freeze_support()
    dirName = "aaa"
    newDirName = "rrr"
    if not os.path.isdir(dirName):
        print("无此文件夹")
        return
    if os.path.exists(newDirName):
        print("目标文件夹已存在")
        return
    os.mkdir(newDirName)
    global q
    q = multiprocessing.Manager().Queue()
    getFilesCount(dirName,newDirName)
    pool = multiprocessing.Pool(processes=3)
    while not q.empty():
        pool.apply_async(func=copyFile, args=(newDirName, q.get(), dirName,))
    pool.close()
    pool.join()
    while not q.empty():
        pass
    print("复制完毕")

if __name__ == "__main__":
    main()

举报

相关推荐

0 条评论