python对文件夹的一些操作

 

代码片段(2)

[代码] 复制文件夹

01def CopyFolderOs(sFolder,tFolder):
02    sourcePath= sFolder
03    destPath= tFolder
04    forroot, dirs, files inos.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        ifnot os.path.isdir(dest):
12            os.mkdir(dest)
13            print'Directory created at: ' + dest
14  
15        #loop through all files in the directory
16        forf in files:
17  
18            #compute current (old) & new file locations
19            oldLoc= root + '\\' + f
20            newLoc= dest + '\\' + f
21  
22            ifnot os.path.isfile(newLoc):
23                try:
24                    shutil.copy2(oldLoc, newLoc)
25                    print'File ' + f + ' copied.'
26                exceptIOError:
27                    print'file "' +f + '" already exists'

[代码] 删除文件夹

1def RemoveFolderOs(sourceDir,localAppDataPath):
2    forroot, dirs, files inos.walk(sourceDir):
3        forf in files:
4            os.unlink(os.path.join(root, f))
5        ford in dirs:
6            shutil.rmtree(os.path.join(root, d))
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值