Python FTP文件定時自動下載實現(xiàn)過程解析
這篇文章主要介紹了Python FTP文件定時自動下載實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
一、需求:
某數(shù)據(jù)公司每日15:00~17:00之間,在其FTP發(fā)布當(dāng)日數(shù)據(jù)供下載,我方需及時下載當(dāng)日數(shù)據(jù)至指定本地目錄。
二、分析:
1、需實現(xiàn)FTP登陸、查詢、下載功能;
解答:使用內(nèi)置的ftplib模塊中FTP類;
2、需判斷文件是否下載;
解答:使用os模塊中path.exists方法;
3、需判斷在指定時間段內(nèi)才執(zhí)行下載任務(wù);
解答:使用內(nèi)置的time模塊抓取當(dāng)前時間,并與指定時間做比較;
4、需考慮日期切換問題;
解答:使用內(nèi)置的time模塊抓取當(dāng)前日期,并與變量中的日期做比較。
三、代碼實現(xiàn)
#!/usr/bin/env python # _*_ coding:utf-8 _*_ ''' @Time : 2019-11-11 13:30 @Author : Peanut_C @FileName: ftp_auto_download.py ''' import time from ftplib import FTP import os remote_path = "/xxx/yy/z/" # 遠(yuǎn)端目錄 begin_time = 1500 # 任務(wù)開始時間 end_time = 1700 # 任務(wù)結(jié)束時間 today = time.strftime("%Y%m%d") # 當(dāng)天日期 today_file = today + 'test.txt' # 得到當(dāng)天日期的目標(biāo)文件名 remote_file = remote_path + today_file # 遠(yuǎn)端文件名 local_file = '\\\\local\\' + today + '\\' + today_file # 本地文件名 log_file = 'C:\\\\log\\ftp_log.txt' def ftp_connect(): """用于FTP連接""" ftp_server = 'w.x.y.z' # ftp站點對應(yīng)的IP地址 username = 'ftpuser' # 用戶名 password = 'ftppass' # 密碼 ftp = FTP() ftp.set_debuglevel(0) # 較高的級別方便排查問題 ftp.connect(ftp_server, 21) ftp.login(username, password) return ftp def remote_file_exists(): """用于FTP站點目標(biāo)文件存在檢測""" ftp = ftp_connect() ftp.cwd(remote_path) # 進(jìn)入目標(biāo)目錄 remote_file_names = ftp.nlst() # 獲取文件列表 ftp.quit() if today_file in remote_file_names: return True else: return False def download_file(): """用于目標(biāo)文件下載""" ftp = ftp_connect() bufsize = 1024 fp = open(local_file, 'wb') ftp.set_debuglevel(0) # 較高的級別方便排查問題 ftp.retrbinary('RETR ' + remote_file, fp.write, bufsize) fp.close() ftp.quit() while True: if int(time.strftime("%H%M")) in range(begin_time, end_time): # 判斷是否在執(zhí)行時間范圍 if int(time.strftime("%Y%m%d")) - int(today) == 0: # 判斷是否跨日期 while not os.path.exists(local_file): # 判斷本地是否已有文件 if remote_file_exists(): # 判斷遠(yuǎn)端是否已有文件 download_file() with open(log_file, 'a') as f: f.write('\n' + time.strftime("%Y/%m/%d %H:%M:%S") + " 今日文件已下載!") time.sleep(60) # 下載完畢靜默1分鐘 else: time.sleep(180) break # 注意,此處跳出循環(huán)重新判斷日期,避免周末或當(dāng)天沒文件時陷入內(nèi)層循環(huán) else: time.sleep(180) else: """如果跨日期,則根據(jù)當(dāng)前日期,更新各文件日期""" today = time.strftime("%Y%m%d") # 當(dāng)天日期 today_file = today + 'test.txt' # 得到當(dāng)天日期的目標(biāo)文件名 remote_file = remote_path + today_file # 遠(yuǎn)端文件名 local_file = '\\\\local\\' + today + '\\' + today_file # 本地文件名 with open(log_file, 'a') as f: f.write('\n' + time.strftime("%Y/%m/%d %H:%M:%S") + " 任務(wù)啟動, 文件日期已更新。") else: time.sleep(1800)
四、運行情況
保存為pyw文件,任務(wù)在后臺持續(xù)運行,不需要計劃任務(wù),省心省力。
不用下載標(biāo)記,一則較為簡潔,二則本地文件如果被人誤刪或移動可自動重新下載。
日志中,每天僅寫入任務(wù)啟動和文件已下載標(biāo)志,并記錄對應(yīng)時間,如有需要可再添加。
希望能幫到有需要的朋友。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python 使用SFTP和FTP實現(xiàn)對服務(wù)器的文件下載功能
- python從ftp獲取文件并下載到本地
- python實現(xiàn)從ftp上下載文件的實例方法
- 基于python實現(xiàn)FTP文件上傳與下載操作(ftp&sftp協(xié)議)
- python實現(xiàn)從ftp服務(wù)器下載文件
- python2.7實現(xiàn)FTP文件下載功能
- python實現(xiàn)下載整個ftp目錄的方法
- python實現(xiàn)的簡單FTP上傳下載文件實例
- python實現(xiàn)支持目錄FTP上傳下載文件的方法
- Python實現(xiàn)FTP文件定時自動下載的步驟
相關(guān)文章
python爬蟲 使用真實瀏覽器打開網(wǎng)頁的兩種方法總結(jié)
下面小編就為大家分享一篇python爬蟲 使用真實瀏覽器打開網(wǎng)頁的兩種方法總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04python調(diào)用API接口實現(xiàn)登陸短信驗證
在本篇文章里小編給大家整理的是關(guān)于python調(diào)用API接口實現(xiàn)登陸短信驗證的實例內(nèi)容,需要的朋友們可以參考下。2020-05-05關(guān)于pytorch中全連接神經(jīng)網(wǎng)絡(luò)搭建兩種模式詳解
今天小編就為大家分享一篇關(guān)于pytorch中全連接神經(jīng)網(wǎng)絡(luò)搭建兩種模式詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01