SoFunction
Updated on 2024-11-14

Python FTP file timed automatic download implementation process analysis

This article introduces the Python FTP file automatically download timed to achieve the process analysis, the text of the sample code through the introduction of the very detailed, for everyone's learning or work has a certain reference to the learning value of the need for friends can refer to the following

I. Demand:

A data company releases the day's data for download on its FTP between 15:00~17:00 every day, and we need to download the day's data to the specified local directory in time.

II. Analysis:

1, need to realize FTP login, query, download function;

Solution: Use the FTP class in the built-in ftplib module;

2. Need to determine whether the file is downloaded;

Solution: Use the methods in the os module;

3, need to judge in the specified time period before executing the download task;

Solution: Use the built-in time module to grab the current time and compare it with the specified time;

4. Date switching needs to be considered;

Solution: Use the built-in time module to grab the current date and compare it with the date in the variable.

III. Code Implementation

#!/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/" # Remote directory
begin_time = 1500 # Start of mission
end_time = 1700 # End of mandate


today = ("%Y%m%d") # Date of the day
today_file = today + '' # Get the target file name for the day's date
remote_file = remote_path + today_file # remote file name
local_file = '\\\\local\\' + today + '\\' + today_file # Local filename
log_file = 'C:\\\\log\\ftp_log.txt'


def ftp_connect():
  """For FTP connections"""
  ftp_server = '' # IP address of the ftp site
  username = 'ftpuser' # Username
  password = 'ftppass' # Password
  ftp = FTP()
  ftp.set_debuglevel(0) # Higher levels facilitate troubleshooting
  (ftp_server, 21)
  (username, password)
  return ftp

def remote_file_exists():
  """For FTP site target file presence detection"""
  ftp = ftp_connect()
  (remote_path) # Go to the target directory
  remote_file_names = () # Get a list of files
  ()
  if today_file in remote_file_names:
    return True
  else:
    return False

def download_file():
  """For target file downloads"""
  ftp = ftp_connect()
  bufsize = 1024
  fp = open(local_file, 'wb')
  ftp.set_debuglevel(0) # Higher levels facilitate troubleshooting
  ('RETR ' + remote_file, , bufsize)
  ()
  ()


while True:
  if int(("%H%M")) in range(begin_time, end_time): # Determine if it's in the execution timeframe
    if int(("%Y%m%d")) - int(today) == 0: # Determine if it crosses dates
      while not (local_file): # Determine if a file already exists locally
        if remote_file_exists(): # Determine if a file already exists on the remote end
          download_file() 
          with open(log_file, 'a') as f:
            ('\n' + ("%Y/%m/%d %H:%M:%S") + " Today's document has been downloaded!")
          (60) # 1 minute of silence after download
        else:
          (180)
          break # Note that the date is re-judged by jumping out of the loop here to avoid getting stuck in an inner loop on weekends or when there are no documents on the day.
      else:
        (180)
    else:
      """If cross-dated, update the date of each document based on the current date"""
      today = ("%Y%m%d") # Date of the day
      today_file = today + '' # Get the target file name for the day's date
      remote_file = remote_path + today_file # remote file name
      local_file = '\\\\local\\' + today + '\\' + today_file # Local filename
      with open(log_file, 'a') as f:
        ('\n' + ("%Y/%m/%d %H:%M:%S") + " Task initiated, file date updated.")
  else:
    (1800)

IV. Operational situation

Save as a pyw file, the task runs continuously in the background, no need for scheduled tasks, save your energy.

There is no need to mark downloads, because it is simpler, and because local files can be automatically re-downloaded if they are mistakenly deleted or moved by someone.

In the log, only the task start and file downloaded flags are written each day, and the corresponding time is recorded, which can be added again if needed.

I hope this helps those who need it.

This is the whole content of this article.