python定时复制远程文件夹中所有文件

yipeiwu_com6年前Python基础

本文实例为大家分享了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\

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

使用python实现kNN分类算法

使用python实现kNN分类算法

k-近邻算法是基本的机器学习算法,算法的原理非常简单: 输入样本数据后,计算输入样本和参考样本之间的距离,找出离输入样本距离最近的k个样本,找出这k个样本中出现频率最高的类标签作为输入样...

python二分查找算法的递归实现方法

本文实例讲述了python二分查找算法的递归实现方法。分享给大家供大家参考,具体如下: 这里先提供一段二分查找的代码: def binarySearch(alist, item):...

python将txt文件读入为np.array的方法

原文件: 7.8094,1.0804,5.7632,0.012269,0.008994,-0.003469,-0.79279,-0.064686,0.11635,0.68827,5....

tensorflow 重置/清除计算图的实现

调用tf.reset_default_graph()重置计算图 当在搭建网络查看计算图时,如果重复运行程序会导致重定义报错。为了可以在同一个线程或者交互式环境中(ipython/jupy...

Django Celery异步任务队列的实现

背景 在开发中,我们常常会遇到一些耗时任务,举个例子: 上传并解析一个 1w 条数据的 Excel 文件,最后持久化至数据库。 在我的程序中,这个任务耗时大约 6s,对于用户来说,...