代码片段(2)
[代码] 复制文件夹
01 | def CopyFolderOs(sFolder,tFolder): |
02 | sourcePath = sFolder |
03 | destPath = tFolder |
04 | for root, dirs, files in os.walk(sourcePath): |
05 | |
06 | #figure out where we're going |
07 | dest = destPath + root.replace(sourcePath, '') |
08 | |
09 | #if we're in a directory that doesn't exist in the destination folder |
10 | #then create a new folder |
11 | if not os.path.isdir(dest): |
12 | os.mkdir(dest) |
13 | print 'Directory created at: ' + dest |
14 | |
15 | #loop through all files in the directory |
16 | for f in files: |
17 | |
18 | #compute current (old) & new file locations |
19 | oldLoc = root + '\\' + f |
20 | newLoc = dest + '\\' + f |
21 | |
22 | if not os.path.isfile(newLoc): |
23 | try : |
24 | shutil.copy2(oldLoc, newLoc) |
25 | print 'File ' + f + ' copied.' |
26 | except IOError: |
27 | print 'file "' + f + '" already exists' |
[代码] 删除文件夹
1 | def RemoveFolderOs(sourceDir,localAppDataPath): |
2 | for root, dirs, files in os.walk(sourceDir): |
3 | for f in files: |
4 | os.unlink(os.path.join(root, f)) |
5 | for d in dirs: |
6 | shutil.rmtree(os.path.join(root, d)) |