Python Forum

Full Version: How to download a list of files from FTP?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I need to download a list of files everyday. These files have the dates in the filenames and I need to pull from 1 - 7 days out.
I have the first part I believe which is this:

import datetime as dt

today = dt.date.today()
dates = []

for i in range(7):
    dates.append(f'ba{str(today + dt.timedelta(days=i))[2:]}.tif'.replace('-', ''))
print(dates)

import ftplib
from ftplib import FTP
import os
# ftp info
HOSTNAME = "ftp.thsite.com"
USERNAME = "myuser"
PASSWORD = "mypass"
savedir = "c:/temp"
os.chdir(savedir)

# connect
ftp = FTP(HOSTNAME, USERNAME, PASSWORD)
ftp.cwd("/Comic_Strips/Baldo")
This returns me a list like this: ['ba220621.tif', 'ba220622.tif', 'ba220623.tif', 'ba220624.tif', 'ba220625.tif', 'ba220626.tif', 'ba220627.tif']

Now to download these I'm getting stuck.

What do i do to start downloading these? I'm embarrassed to put my code for this up cause i'm sure it makes no sense, but here goes.

# where it falls off the rails... i obviously am on the wrong track here...
fn = ("dates")
file = open(dates, "wb")
ftp.retrbinary("RETR %s" + dates, file.write)
Appreciate any help out there!