在文件同级目录下存在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()