• python查找文件、移动、重命名、压缩


    在文件同级目录下存在sourcefile、targetfile两个文件夹,源文件放在sourcefile

    import os
    import shutil
    
    Apath = os.path.dirname(os.path.abspath(__file__))
    
    #查找文件复制到指定路径并重命名
    def replacefile():
        fs = ["rcds-dao-1.5.0-RELEASE.jar",
              "rcds-biz-1.5.0-RELEASE.jar",
              "rcds-type-1.5.0-RELEASE.jar",
              "rcds-service-1.5.0-RELEASE.jar",
              "rdsmnt-core-1.5.0-RELEASE.jar",
              "rdsmnt-web-1.5.0-RELEASE.jar",
              "rdsos-dao-1.5.0-RELEASE.jar",
              "rdsos-engine-1.5.0-RELEASE.jar",
              "rdsos-core-1.5.0-RELEASE.jar",
              "rdsos-cache-1.5.0-RELEASE.jar",
              "rdsos-biz-1.5.0-RELEASE.jar",
              "rdsos-service-1.5.0-RELEASE.jar",
              "dss-dao-1.5.0-RELEASE.jar",
              "dss-core-1.5.0-RELEASE.jar",
              "dss-type-1.5.0-RELEASE.jar",
              "ps-core-1.5.0-RELEASE.jar",
              "ps-dao-1.5.0-RELEASE.jar",
              "ps-client-1.5.0-RELEASE.jar",
              "rds-cache-1.5.0-RELEASE.jar",
              "rds-engine-1.5.0-RELEASE.jar",
              "rds-redis-1.5.0-RELEASE.jar",
              "rds-core-1.5.0-RELEASE.jar",
              "rds-common-1.5.0-RELEASE.jar",
              "rds-biz-1.5.0-RELEASE.jar",
              "rds-dao-1.5.0-RELEASE.jar",
              "rds-service-1.5.0-RELEASE.jar",
              "rcds-.zip",
              "rdsmnt-.zip",
              "rdsos-.zip",
              "dss-.zip",
              "ps-.zip",
              "rds-.zip"]
    
        fsre = ["rcds-dao.jar",
                "rcds-biz.jar",
                "rcds-type.jar",
                "rcds-service.jar",
                "rdsmnt-core.jar",
                "rdsmnt-web.jar",
                "rdsos-dao.jar",
                "rdsos-engine.jar",
                "rdsos-core.jar",
                "rdsos-cache.jar",
                "rdsos-biz.jar",
                "rdsos-service.jar",
                "dss-dao.jar",
                "dss-core.jar",
                "dss-type.jar",
                "ps-core.jar",
                "ps-dao.jar",
                "ps-client.jar",
                "rds-cache.jar",
                "rds-engine.jar",
                "rds-redis.jar",
                "rds-core.jar",
                "rds-common.jar",
                "rds-biz.jar",
                "rds-dao.jar",
                "rds-service.jar",
                "rcds-war.jar",
                "rdsmnt-war.jar",
                "rdsos-war.jar",
                "dss-war.jar",
                "ps-war.jar",
                "rds-war.jar"]
    
        path = Apath +"\sourcefile"
        newpath = Apath +"\targetfile"
        os.system("del /f /q %s" % newpath) #清空目标文件夹
    
        i = 0
        for i in range(len(fs)):
            name = fs[i]
            rename =fsre[i]
            oldpath = search(path, name)
            #print(oldpath)
            if oldpath != None:
                shutil.copy(oldpath, newpath)
                os.chdir(newpath) #进入指定目录
                os.rename(name,rename) #重命名
    
    
     #查找文件并返回绝对路径
    def search(path, name):
        for item in os.listdir(path):
            item_path = os.path.join(path, item)
            if os.path.isdir(item_path):
                  result = search(item_path, name)
                  if result:
                      return result
            elif os.path.isfile(item_path):
                if name == item:
                    oldpath = item_path
                    #print(oldpath)
                    return oldpath
    
    #压缩文件夹
    def filezip():
        zs = ["rcds-",
              "rdsmnt-",
              "rdsos-",
              "dss-",
              "ps-",
              "rds-"]
    
        path = Apath + "\sourcefile"
    
        m = 0
        for item in os.listdir(path):
            item_path = os.path.join(path, item)
            if os.path.isdir(item_path):
                for m in range(len(zs)):
                    name = zs[m]
                    if name in item:
                        #print(name)
                        if name =="rdsmnt-":
                            basepath = item_path
                            #print(item_path)
                            base_dir = basepath + "\WEB-INF\classes\com\suning\framework"
                            base_name = basepath +"\"+ name
                            shutil.make_archive(base_name, 'zip', base_dir)
                        elif name !="rdsmnt-":
                            basepath = item_path
                            base_dir = basepath + "\WEB-INF\classes\com\suning\snb"
                            base_name = basepath +"\"+ name
                            shutil.make_archive(base_name, 'zip', base_dir)
    
    
    if __name__ == '__main__':
        filezip()
        replacefile()
  • 相关阅读:
    linux-cp
    linux-rmdir
    linux-tail
    linux-head
    linux 重定向
    hadoop环境安装及错误总结
    vim 图册
    为什么可以这么快! awk 与python的应用
    八大排序算法的 Python 实现
    linux的文件隐藏属性 chattr lsattr
  • 原文地址:https://www.cnblogs.com/dingxiansheng/p/15398776.html
Copyright © 2020-2023  润新知