使用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怎么定時復制遠程文件夾中的所有文件的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創新互聯成都網站設計公司行業資訊頻道,感謝各位的閱讀!
另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
文章題目:使用python怎么定時復制遠程文件夾中的所有文件-創新互聯
文章地址:http://vcdvsql.cn/article4/djggie.html
成都網站建設公司_創新互聯,為您提供微信小程序、網站設計公司、網站維護、營銷型網站建設、建站公司、網站策劃
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯