zoukankan      html  css  js  c++  java
  • python26实例[subst然后删除文件夹]

    有些时候某些文件夹下的文件路径太长,超出了windows系统的限制,导致此文件夹不能被删除。 此时我们需要先subst此文件夹然后再删除,以下脚本帮你自动完成。

    代码:

    import os
    import sys
    import shutil
    import subprocess
      
    def runCommand(cmd):
      
    return subprocess.call(cmd)

    def substDriveForPath(drive, path):
      substcmd 
    = "subst" + " " + drive + " " + path
      
    return runCommand(substcmd)
      
    def unSubstDriveForPath(drive):
      unsubstcmd 
    = "subst" + " " +drive + " " + "/d"
      runCommand(unsubstcmd)
      
    def autoSubst(path):
      result 
    = False
      
      useddrive 
    = ""
      
    for drive in "ZYXWVUTSRQPONMLKJIHGFEDCBA":
        fulldrive 
    = drive + ":"
        returncode 
    = substDriveForPath(fulldrive,path)
        
    if(returncode == 0):
          useddrive 
    = fulldrive
          
    break
      
    if(not useddrive == ""):
        result 
    = True
        
      
    return result, useddrive

    def rmDirwithSubst(dir, subst = True):

      
    if not os.path.exists(dir) and not os.path.isdir(dir) : 
            
    print 'path %s is not existed or is not a directory' %dir
            
    return False
      
      
    if subst:
        parent, curdir 
    = os.path.split(dir)
        re, d 
    = autoSubst(parent)
        
    if re == False :
          
    print  'subst failed'
          
    return False
        dir 
    = d + '\\' + curdir
      
      shutil.rmtree(dir)
      unSubstDriveForPath(d)
        
      
    return True
      
    def usage():
      usage 
    = '\
      When the dirctory cannot be removed in which some files path are too long.\n\
      Usage: python %s folder1 [folder2 ...]\n
    ' %sys.argv[0]
      
    print usage


    if __name__ == '__main__':
      
    if len(sys.argv) < 2 : usage()
      
    for folder in sys.argv[1:]:
        rmDirwithSubst(folder)

    完!

  • 相关阅读:
    c#常用的技巧
    在Web应用程序中使用Castle ActiveRecord
    Castle Query返回System.String程序报错的解决方法
    C++标准转换运算符const_cast
    Linux目录解释
    [转载]存储过程与函数的区别
    详解GCC的下载和安装
    Linux控制台的快捷键
    Linux下软件的安装和卸载
    电路交换,报文交换和分组交换
  • 原文地址:https://www.cnblogs.com/itech/p/1991758.html
Copyright © 2011-2022 走看看