python定时复制远程文件夹中所有文件
作者:weareu 时间:2023-08-17 17:55:32
本文实例为大家分享了python定时复制远程文件夹中文件的具体代码,供大家参考,具体内容如下
import os, shutil, sys
import threading
import configparser
import 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 = True
shutil.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).seconds
print(end_time, " 本次执行完毕,等待", time, "秒……")'''
print(end_time, " 本次执行完毕,等待", span, "秒……")
#定时复制
def timer_copy(src_path, dst_path):
remote_copy(src_path, dst_path)
global timer
timer = 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 span
span = config.getint('run', 'timeSpan')
global file_size
file_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=20000
fileSize=5000
[path]
srcPath=\\192.168.0.108\xxxx\
dstPath=f:\downloads\
来源:https://blog.csdn.net/weareu/article/details/80427311
标签:python,定时复制
0
投稿
猜你喜欢
5步让你的CSS样式表成功减肥
2009-08-02 21:27:00
利用Python自动监控网站并发送邮件告警的方法
2023-12-30 03:39:41
Python爬虫之用Xpath获取关键标签实现自动评论盖楼抽奖(二)
2021-02-11 00:58:03
Python中numpy模块常见用法demo实例小结
2022-08-03 12:45:12
MySQL使用索引优化性能
2024-01-14 19:11:21
对tensorflow中tf.nn.conv1d和layers.conv1d的区别详解
2022-07-17 02:28:27
IE7下动态创建Iframe时,去除边框的办法
2009-01-19 13:56:00
asp如何分页显示数据库查询结果?
2009-11-22 19:23:00
基于Python实现抢注大词的提词工具
2022-04-07 02:00:20
Python 阶乘详解
2022-01-16 13:56:00
python 监控服务器是否有人远程登录(详细思路+代码)
2022-05-16 04:08:26
echarts柱状堆叠图实现示例(图例和x轴都是动态的)
2024-04-29 13:21:03
加固SQL参数与存储过程
2012-03-12 19:44:08
Python如何定义有默认参数的函数
2023-08-05 14:38:30
W3C 接连推出 7 个 HTML 草案
2010-03-10 10:37:00
详解python的xlwings库读写excel操作总结
2021-03-25 02:00:00
Python异步操作MySQL示例【使用aiomysql】
2024-01-16 20:22:07
Python入门之集合的使用教程
2022-10-31 13:44:45
基于Go语言实现插入排序算法及优化
2024-05-22 10:18:05
通过 for 循环比较 Python 与 Ruby 的编程区别
2022-11-12 01:19:26