python递归删除非空文件夹
默认分类 2012-07-24 21:16:15
python递归删除非空文件夹
-
-
-
- import os
- import codecs
- import shutil
- import sys
- import stat
-
- def deleteSubFile(path):
- names = os.listdir(path)
- for name in names:
- fp = os.path.join( path, name)
- if (os.path.isfile(fp)):
- os.chmod( fp, stat.S_IWRITE)
- os.remove(fp)
- else:
- deleteSubFile(fp)
-
- def deletePath(parentPath = None, dir = None):
- if (dir != None):
- deleteSubFile(os.path.join( parentPath, dir))
- shutil.rmtree(os.path.join( parentPath, dir), True, False)
- print 'deleted ', os.path.join( parentPath, dir)
- else:
- if (dir != None):
- filePath = os.path.join( parentPath, dir)
- else:
- filePath = parentPath
- names = os.listdir(filePath)
- for name in names:
- fp = os.path.join( filePath, name)
- if (os.path.isdir(fp)):
- deletePath(filePath, name)
-
- if os.path.exists('output'):
- deletePath('output')
- shutil.rmtree('output')
-
- shutil.copytree('html', 'output')
- os.chdir('output')
- os.popen('python build.py')
>>
留言评论