文件数据备份脚本

目的:实现多层文件夹文件的检索,筛选需要的文件,完成备份路径的创建(根据时间和传参[项目名 版本名 等]),完成文件的copy,然后对备份的所有文件进行压缩

py文件增加了3个方法:

1.路径创建

2.文件检索与copy

3.文件压缩

 

使用.bat文件带3个参数调用该py文件

  1 #! /usr/bin/env python
  2 #coding=utf-8
  3 #Edit:Sandy Zheng
  4 #date:2021/7/6
  5 
  6 import os
  7 import time
  8 import os, shutil
  9 import os.path
 10 import time
 11 from pathlib import Path
 12 import zipfile
 13 import sys
 14 
 15 Projectname=sys.argv[1]
 16 Type =sys.argv[2]
 17 VERSION=sys.argv[3]
 18 
 19 # 格式化
 20 CurrentTime = (time.strftime("%Y-%m-%d %H-%M-%S", time.localtime()))
 21 
 22 CurrentTime1 = (time.strftime("%Y %m %d %H %M %S", time.localtime()))
 23 print (CurrentTime)
 24 def mkdir(path):
 25 
 26     # 去除首位空格
 27     path = path.strip()
 28     # 去除尾部  符号
 29     path = path.rstrip("\")
 30 
 31     # 判断路径是否存在
 32     # 存在     True
 33     # 不存在   False
 34     isExists = os.path.exists(path)
 35 
 36     # 判断结果
 37     if not isExists:
 38         # 如果不存在则创建目录
 39         # 创建目录操作函数
 40         os.makedirs(path)
 41 
 42         print(path + ' 创建成功')
 43         return True
 44     else:
 45         # 如果目录存在则不创建,并提示目录已存在
 46         print(path + ' 目录已存在')
 47         return False
 48 
 49 def copy_search_file(srcDir, desDir):
 50     ls = os.listdir(srcDir)
 51     for line in ls:
 52         filePath = os.path.join(srcDir, line)
 53         if os.path.isfile(filePath):
 54             shutil.copy(filePath, desDir)
 55 
 56         if os.path.isdir(filePath) and not(filePath.endswith("ExtFlash")):
 57             ls1 = os.listdir(filePath)
 58             for line1 in ls1:
 59                 filePath2 = os.path.join(filePath,line1)
 60                 if os.path.isfile(filePath2):
 61                     mkdir(desDir+"\"+line)
 62                     shutil.copy(filePath2,desDir+"\"+line)
 63 
 64             if Path(filePath + "\ExtFlash").exists():
 65                 ls3 = os.listdir(filePath + "\ExtFlash")
 66                 for line2 in ls3:
 67                     filePath0 = os.path.join(filePath + "\ExtFlash", line2)
 68                     mkdir(filePath+"\"+line)
 69                     if os.path.isfile(filePath0):
 70                         if filePath0.endswith(".hex"):
 71                             shutil.copy(filePath0, desDir+"\"+line)
 72 
 73     if Path(srcDir+"\ExtFlash").exists():
 74         ls = os.listdir(srcDir+"\ExtFlash")
 75         for line in ls:
 76             filePath = os.path.join(srcDir+"\ExtFlash", line)
 77             if os.path.isfile(filePath):
 78                 if filePath.endswith(".hex"):
 79                     shutil.copy(filePath, desDir)
 80 
 81 def zip_ya(startdir,file_news):
 82     #startdir要压缩的文件夹路径
 83     #file_news是要压缩后压缩文件名,需要带后缀
 84     z = zipfile.ZipFile(file_news,'w',zipfile.ZIP_DEFLATED) #参数一:文件夹名
 85     for dirpath, dirnames, filenames in os.walk(startdir):
 86         fpath = dirpath.replace(startdir,'') #这一句很重要,不replace的话,就从根目录开始复制
 87         fpath = fpath and fpath + os.sep or ''#,实现当前文件夹以及包含的所有文件的压缩
 88         for filename in filenames:
 89             z.write(os.path.join(dirpath, filename),fpath+filename)
 90             print ('压缩成功')
 91     z.close()
 92 
 93 #copy_search_file("D:\test\src","D:\test\out")
 94 #zip_ya("D:\test\out","D:\test\out"+".zip")
 95 
 96 
 97 FinalPath = "D:FirmwareBackUp"+"\"+Projectname+"\"+CurrentTime+"\"+Type+VERSION
 98 print ("固件备份路径是:"+FinalPath)
 99 mkdir(FinalPath)
100 copy_search_file(".\"+"\out" ,FinalPath)
101 zip_ya(FinalPath,FinalPath+".zip")
102 oldname = FinalPath+".zip"
103 newname = Projectname+" "+CurrentTime+" "+Type+VERSION+".zip"
104 os.rename(oldname,newname)
105 shutil.copy(newname,".\"+"\out")
106 shutil.move(newname,FinalPath)
原文地址:https://www.cnblogs.com/Sandy-1128/p/sandy1128-backup.html