python定時(shí)復(fù)制遠(yuǎn)程文件夾中所有文件
更新時(shí)間:2019年04月30日 11:04:07 作者:weareu
這篇文章主要為大家詳細(xì)介紹了python定時(shí)復(fù)制遠(yuǎn)程文件夾中所有文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了python定時(shí)復(fù)制遠(yuǎn)程文件夾中文件的具體代碼,供大家參考,具體內(nèi)容如下
import os, shutil, sys import threading import configparser import datetime #復(fù)制文件 def remote_copy(src_path, dst_path): start_time = datetime.datetime.now() print(start_time, " 開始復(fù)制……") 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("無法發(fā)現(xiàn)文件,請檢查網(wǎng)絡(luò)連接!") os.system('pause') sys.exit() else: if not bCopy: print("未發(fā)現(xiàn)新文件……") end_time = datetime.datetime.now() ''' time = span - (end_time - start_time).seconds print(end_time, " 本次執(zhí)行完畢,等待", time, "秒……")''' print(end_time, " 本次執(zhí)行完畢,等待", span, "秒……") #定時(shí)復(fù)制 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("執(zhí)行間隔為 :", span) print("文件限制為 :", file_size) print("輸入文件夾為:", src_path) print("輸出文件夾為:", dst_path) inp = input("是否繼續(xù)(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\
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python獲取遠(yuǎn)程文件大小的函數(shù)代碼分享
- python實(shí)現(xiàn)帶錯(cuò)誤處理功能的遠(yuǎn)程文件讀取方法
- python通過paramiko復(fù)制遠(yuǎn)程文件及文件目錄到本地
- python使用Paramiko模塊實(shí)現(xiàn)遠(yuǎn)程文件拷貝
- python 從遠(yuǎn)程服務(wù)器下載日志文件的程序
- Python通過paramiko遠(yuǎn)程下載Linux服務(wù)器上的文件實(shí)例
- python實(shí)現(xiàn)下載文件的三種方法
- Python實(shí)現(xiàn)HTTP協(xié)議下的文件下載方法總結(jié)
- Python實(shí)現(xiàn)批量下載文件
- Python實(shí)現(xiàn)的遠(yuǎn)程文件自動打包并下載功能示例
相關(guān)文章
python定時(shí)檢測無響應(yīng)進(jìn)程并重啟的實(shí)例代碼
這篇文章主要介紹了python定時(shí)檢測無響應(yīng)進(jìn)程并重啟的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04python進(jìn)程的狀態(tài)、創(chuàng)建及使用方法詳解
這篇文章主要介紹了python進(jìn)程的狀態(tài)、創(chuàng)建及使用方法,結(jié)合實(shí)例形式詳細(xì)分析了Python進(jìn)程的概念、原理、工作狀態(tài)、創(chuàng)建以及使用方法,需要的朋友可以參考下2019-12-12python進(jìn)階學(xué)習(xí)實(shí)時(shí)目標(biāo)跟蹤示例詳解
這篇文章主要為大家介紹了python進(jìn)階學(xué)習(xí)實(shí)時(shí)目標(biāo)跟蹤示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Python設(shè)計(jì)模式結(jié)構(gòu)型組合模式
這篇文章主要介紹了Python設(shè)計(jì)模式結(jié)構(gòu)型組合模式,組合模式即Composite?Pattern,將對象組合成成樹形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu),組合模式使得用戶對單個(gè)對象和組合對象的使用具有一致性,下文具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-02-02python3實(shí)現(xiàn)公眾號每日定時(shí)發(fā)送日報(bào)和圖片
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)公眾號每日定時(shí)發(fā)送日報(bào)和圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02