The latest project is to move the entire folder in one directory to another directory. The folder contains many layers of folders, and the most rare one is that if there are folders with the same name under different folders, move them to the same directory, you must integrate all the contents of folders with the same name. For example:
Directory D contains the following content: (D:/src/)
D:/src/B/)
Directory E:/DST
To add the contents in the D:/src/a folder (including folders and files), D:/src/B/A (including folders and files)
(Note: There may be files with the same name under the folder)
Move to E:/DST
You can write a function to implement the above functions:
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