Python中整个文件夹的移动


最近做个项目是将一个目录下整个文件夹移到另外一个目录下。其中文件夹下又套了很多层文件夹,还有最难得一点是文件夹在不同的文件夹下如果有同名的文件夹,要移到同一个目录下,又必须将同名的文件夹得所有内容整合在一起,例如:

目录D盘下又如下内容:(D:/src/a  D:/src/b/a) 

目录E盘:E:/dst

要将D:/src/a文件夹下的内容(含有文件夹和文件)、D:/src/b/a(含有的文件夹和文件)

(备注:文件夹下可能还有同名的)

移到E:/dst下

自己写了一个函数可以实现以上功能:

Def  MoveDirAndFiles(srcPath,dstPath)

       allChangeFileList=os.listdir(srcPath)

       for changeFileItem in allChangeFileList:

              changeFilePath=os.path.join(srcPath,changeFileItem)

                     if os.path.isdir(changeFilePath):

                            dstAddPath=os.path.join.(dstPath,changeFileItem)

                                   if os.path.exits(dstAddPath):

                                          MoveDirAndFiles(changeFilePath,dstAddPath)

                                   Else:

                                          Shutil.copytree(changeFilePath,dstAddPath,False)

                                          Shutil.retree(changeFilePath)

                     Else:

                            Shutil.move(changeFilePath,dstPath)

       Os.rmdir(srcPath)

       Return Ture

相关内容