python定时复制远程文件夹中所有文件
Python  /  管理员 发布于 7年前   286
本文实例为大家分享了python定时复制远程文件夹中文件的具体代码,供大家参考,具体内容如下
import os, shutil, sysimport threadingimport configparserimport datetime #复制文件def remote_copy(src_path, dst_path): start_time = datetime.datetime.now()print(start_time, " 开始复制……")bCopy = False;try:# 获取源文件夹中的所有文件及文件夹files = os.listdir(src_path)for file in files:#生成绝对路径src_file = os.path.join(src_path,file)# 判断是否为文件if os.path.isfile(src_file) and (os.path.getsize(src_file) < file_size) : dst_file = os.path.join(dst_path,file)if not os.path.exists(dst_file):bCopy = Trueshutil.copyfile(src_file, dst_file)'''copy_cmd = 'xcopy /D %s %s'%(src_file,dst_file)os.popen(copy_cmd)'''print(src_file, ' => ', dst_file, 'copy done!')#else:# print(dst_file, "已存在!") except Exception as e:print("无法发现文件,请检查网络连接!")os.system('pause')sys.exit() else:if not bCopy:print("未发现新文件……")end_time = datetime.datetime.now()'''time = span - (end_time - start_time).secondsprint(end_time, " 本次执行完毕,等待", time, "秒……")''' print(end_time, " 本次执行完毕,等待", span, "秒……") #定时复制def timer_copy(src_path, dst_path):remote_copy(src_path, dst_path) global timertimer = threading.Timer(span, timer_copy, [src_path, dst_path])timer.start() # 程序入口if __name__ == "__main__": #读取配置文件config = configparser.ConfigParser()config.read("config.ini") src_path = config.get('path', 'srcPath')dst_path = config.get('path', 'dstPath')global spanspan = config.getint('run', 'timeSpan')global file_sizefile_size = config.getint('run', 'fileSize') # 目的路径不存在则建立路径if not os.path.exists(dst_path):os.makedirs(dst_path) print("配置文件为 :config.ini")print("执行间隔为 :", span)print("文件限制为 :", file_size)print("输入文件夹为:", src_path)print("输出文件夹为:", dst_path) inp = input("是否继续(y/n):")if inp == 'y' or inp == 'Y': timer = threading.Timer(1, timer_copy, [src_path, dst_path])timer.start() #测试#remote_copy(src_path, dst_path)else:sys.exit()
配置文件config.ini
[run]timeSpan=20000fileSize=5000[path]srcPath=\\192.168.0.108\xxxx\dstPath=f:\downloads\
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号