当前位置:脚本大全 > > 正文

python带权限复制文件夹(python定时复制远程文件夹中所有文件)

时间:2021-10-12 00:53:53类别:脚本大全

python带权限复制文件夹

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

本文实例为大家分享了python定时复制远程文件夹中文件的具体代码,供大家参考,具体内容如下

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 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

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • [run]
  • timespan=20000
  • filesize=5000
  • [path]
  • srcpath=\\192.168.0.108\xxxx\
  • dstpath=f:\downloads\
  • 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。

    原文链接:https://blog.csdn.net/weareu/article/details/80427311

    上一篇下一篇

    猜您喜欢

    热门推荐