导航栏: 首页 评论列表

python递归删除非空文件夹

默认分类 2012-07-24 21:16:15

python递归删除非空文件夹

  1. #!/usr/bin/python  
  2. # -*- coding: utf-8 -*-  
  3.   
  4. import os  
  5. import codecs  
  6. import shutil  
  7. import sys  
  8. import stat  
  9.   
  10. def deleteSubFile(path):  
  11.     names = os.listdir(path)  
  12.     for name in names:  
  13.         fp = os.path.join( path, name)  
  14.         if (os.path.isfile(fp)):  
  15.             os.chmod( fp, stat.S_IWRITE)  
  16.             os.remove(fp)  
  17.         else:  
  18.             deleteSubFile(fp)  
  19.   
  20. def deletePath(parentPath = None, dir = None):  
  21.     if (dir != None):  
  22.         deleteSubFile(os.path.join( parentPath, dir))  
  23.         shutil.rmtree(os.path.join( parentPath, dir), True, False)  
  24.         print 'deleted ', os.path.join( parentPath, dir)  
  25.     else:  
  26.         if (dir != None):  
  27.             filePath = os.path.join( parentPath, dir)  
  28.         else:  
  29.             filePath = parentPath  
  30.         names = os.listdir(filePath)  
  31.         for name in names:  
  32.             fp = os.path.join( filePath, name)  
  33.             if (os.path.isdir(fp)):  
  34.                 deletePath(filePath, name)  
  35.   
  36. if os.path.exists('output'):  
  37.     deletePath('output')  
  38.     shutil.rmtree('output')  
  39.   
  40. shutil.copytree('html''output')  
  41. os.chdir('output')  
  42. os.popen('python build.py')  


>> 留言评论