python 批量复制不同目录下的文件

2023-01-31 02:01:49 文件 复制 批量

python 批量复制不同目录下的文件

#!/usr/bin/env Python
#coding=UTF-8
#author aping

import os,sys,shutil,time
import getpass,os.path
from ftplib import FTP

back01="/fs/vm/vm-back/tes.vmdk"
back02="/tmp/111.vmdk"
back03="/fs/222.vmdk"

tmpFileDir = "/tmp/"                #复制临时存储目录
serverName = "BWA123"          #资产编号(主机名)
logPath = "/tmp/log.txt"           #日志文件路径

#记录日志函数
def log(msg=''):
    log = open( logPath,"a")
    log.writelines(msg+'\n')
    log.close()
   
def lzp(sourceFileName='', sourceDir='', serverName=''):
    date = time.strftime('%Y.%m.%d',time.localtime())
    tmpFileName=date+'-'+serverName+'-'+sourceFileName+'.tmp'
    backFile = tmpFileDir+tmpFileName
    #01stage_copy
    if os.path.isfile(sourceDir+sourceFileName):
        dateTime = time.strftime('%Y-%m-%d %H:%M',time.localtime())
        shutil.copyfile(sourceDir+sourceFileName,backFile)#执行: 复制
        log(str(dateTime+" 本地拷贝完成 "+backFile))
    else:
        dateTime = time.strftime('%Y-%m-%d %H:%M',time.localtime())
        log(str(dateTime+" 本地拷贝失败 "+sourceDir+sourceFileName))
           
def main():
    b = [back01,back02,back03]
    log(str(" -----------------------------------------"))
    for fileSet in b:
        tmpPat,sourceFileName=os.path.split(fileSet) #路径文件分割
        sourceDir=tmpPat+"/"
        dateTime = time.strftime('%Y-%m-%d %H:%M',time.localtime())
        log(str(dateTime+" 备份的源文件 "+fileSet))
        lzp(sourceFileName,sourceDir,serverName)
        log(str(" -----------------------------------------"))
if __name__ == "__main__":
    main()

相关文章