Python FTP文件定时自动下载实现过程解析
作者:Peanut_C 时间:2023-04-30 19:08:51
这篇文章主要介绍了Python FTP文件定时自动下载实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
一、需求:
某数据公司每日15:00~17:00之间,在其FTP发布当日数据供下载,我方需及时下载当日数据至指定本地目录。
二、分析:
1、需实现FTP登陆、查询、下载功能;
解答:使用内置的ftplib模块中FTP类;
2、需判断文件是否下载;
解答:使用os模块中path.exists方法;
3、需判断在指定时间段内才执行下载任务;
解答:使用内置的time模块抓取当前时间,并与指定时间做比较;
4、需考虑日期切换问题;
解答:使用内置的time模块抓取当前日期,并与变量中的日期做比较。
三、代码实现
#!/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/" # 远端目录
begin_time = 1500 # 任务开始时间
end_time = 1700 # 任务结束时间
today = time.strftime("%Y%m%d") # 当天日期
today_file = today + 'test.txt' # 得到当天日期的目标文件名
remote_file = remote_path + today_file # 远端文件名
local_file = '\\\\local\\' + today + '\\' + today_file # 本地文件名
log_file = 'C:\\\\log\\ftp_log.txt'
def ftp_connect():
"""用于FTP连接"""
ftp_server = 'w.x.y.z' # ftp站点对应的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站点目标文件存在检测"""
ftp = ftp_connect()
ftp.cwd(remote_path) # 进入目标目录
remote_file_names = ftp.nlst() # 获取文件列表
ftp.quit()
if today_file in remote_file_names:
return True
else:
return False
def download_file():
"""用于目标文件下载"""
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): # 判断是否在执行时间范围
if int(time.strftime("%Y%m%d")) - int(today) == 0: # 判断是否跨日期
while not os.path.exists(local_file): # 判断本地是否已有文件
if remote_file_exists(): # 判断远端是否已有文件
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 # 注意,此处跳出循环重新判断日期,避免周末或当天没文件时陷入内层循环
else:
time.sleep(180)
else:
"""如果跨日期,则根据当前日期,更新各文件日期"""
today = time.strftime("%Y%m%d") # 当天日期
today_file = today + 'test.txt' # 得到当天日期的目标文件名
remote_file = remote_path + today_file # 远端文件名
local_file = '\\\\local\\' + today + '\\' + today_file # 本地文件名
with open(log_file, 'a') as f:
f.write('\n' + time.strftime("%Y/%m/%d %H:%M:%S") + " 任务启动, 文件日期已更新。")
else:
time.sleep(1800)
四、运行情况
保存为pyw文件,任务在后台持续运行,不需要计划任务,省心省力。
不用下载标记,一则较为简洁,二则本地文件如果被人误删或移动可自动重新下载。
日志中,每天仅写入任务启动和文件已下载标志,并记录对应时间,如有需要可再添加。
希望能帮到有需要的朋友。
来源:https://www.cnblogs.com/baitme/p/11837017.html
标签:python,ftp,文件,定时,自动,下载
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如果用JS得到字符串中出现次数最多的字母
2007-12-03 21:01:00
vue 图片裁剪上传组件的实现
2024-05-09 15:14:02
![](https://img.aspxhome.com/file/2023/4/126544_0s.gif)
和server XMLHTTP相关的几个函数
2008-09-21 13:56:00
Pyscript使用本地Pyodide配置步骤
2021-06-12 06:58:49
Python读取word文本操作详解
2023-08-23 15:32:50
![](https://img.aspxhome.com/file/2023/3/61993_0s.jpg)
python中fastapi设置查询参数可选或必选
2021-11-04 12:25:18
教你怎么用Python处理excel实现自动化办公
2023-11-05 02:19:50
![](https://img.aspxhome.com/file/2023/1/64301_0s.png)
Go语言struct要使用 tags的原因解析
2023-08-31 09:25:19
![](https://img.aspxhome.com/file/2023/6/93666_0s.jpg)
Python实战整活之聊天机器人
2022-10-13 09:57:50
![](https://img.aspxhome.com/file/2023/6/105806_0s.png)
简单介绍Python中的len()函数的使用
2022-08-19 16:27:02
浅述python中深浅拷贝原理
2022-03-05 06:29:36
mysql查询条件not in 和 in的区别及原因说明
2024-01-27 12:41:49
![](https://img.aspxhome.com/file/2023/3/95623_0s.jpg)
setTimeout与setInterval在不同浏览器下的差异
2024-04-23 09:23:54
一些 T-SQL 技巧
2024-01-26 00:21:16
DOS命令行窗口mysql中文显示乱码问题解决方法
2024-01-26 13:53:44
![](https://img.aspxhome.com/file/2023/1/119771_0s.gif)
python3 assert 断言的使用详解 (区别于python2)
2023-03-10 03:05:26
python实现基于SVM手写数字识别功能
2021-10-03 12:33:41
![](https://img.aspxhome.com/file/2023/9/133329_0s.jpg)
vue-swiper的使用教程
2024-04-27 16:17:56
基于Python实现原创程序猿乘风破浪小游戏
2021-03-09 01:41:51
![](https://img.aspxhome.com/file/2023/0/106970_0s.png)
php封装的单文件(图片)上传类完整实例
2024-05-03 15:50:18