Python中整个文件夹的移动

来源:LinuxIDC.com 作者:LinuxIDC.com
  

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

目录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


时间:2011-12-17 14:33 来源:LinuxIDC.com 作者:LinuxIDC.com 原文链接

好文,顶一下
(0)
0%
文章真差,踩一下
(0)
0%
------分隔线----------------------------


把开源带在你的身边-精美linux小纪念品
无觅相关文章插件,快速提升流量