使用python怎么定時復制遠程文件夾中的所有文件-創(chuàng)新互聯(lián)

使用python怎么定時復制遠程文件夾中的所有文件,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

創(chuàng)新互聯(lián)建站堅信:善待客戶,將會成為終身客戶。我們能堅持多年,是因為我們一直可值得信賴。我們從不忽悠初訪客戶,我們用心做好本職工作,不忘初心,方得始終。十年網(wǎng)站建設經(jīng)驗創(chuàng)新互聯(lián)建站是成都老牌網(wǎng)站營銷服務商,為您提供網(wǎng)站制作、成都網(wǎng)站制作、網(wǎng)站設計、HTML5、網(wǎng)站制作、品牌網(wǎng)站建設、重慶小程序開發(fā)服務,給眾多知名企業(yè)提供過好品質(zhì)的建站服務。

具體內(nèi)容如下

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("無法發(fā)現(xiàn)文件,請檢查網(wǎng)絡連接!")
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, "秒……")
 

#定時復制
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)容,你們掌握使用python怎么定時復制遠程文件夾中的所有文件的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設計公司行業(yè)資訊頻道,感謝各位的閱讀!

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

文章題目:使用python怎么定時復制遠程文件夾中的所有文件-創(chuàng)新互聯(lián)
文章地址:http://muchs.cn/article4/djggie.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信小程序、網(wǎng)站設計公司、網(wǎng)站維護營銷型網(wǎng)站建設、建站公司、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)