- Connect to an ftp site
- Change to the appropriate directory
- Get a listing
- Use a Regex to pull out just the file names from the listing
- Cycle through the file names and download each one
Here is the code I have so far:
#!/usr/bin/env python
import ftplib
import re
ftp = ftplib.FTP("ftp.fu-berlin.de")
ftp.login('anonymous', 'anything')
# The directory to change to, once logged in
directory = '/pub/misc/movies/database/'
# Now to change directories
ftp.cwd(directory)
# This will handle the data being downloaded
# It will be explained shortly
def handleDownload(block):
file.write(block)
print ".",
data = []
# Print the contents of the directory
ftp.retrlines('LIST *.gz', data.append)
# Setup the regular expression used to match the file name from the listing
prog = re.compile('\s+(?P<name>[a-zA-Z0-9\-\.]+)$')
result = []
for line in data:
result = prog.search(line)
print result.group(0)
At this point, this will print the entire list of names from the site shown. (The login/pwd shown work as it is a public ftp site)
The above prints the list to show that it is there. Now, if you replace the final for loop above with the following:
for filename in result.group():
print 'Opening local file ' + filename
file = open(filename, 'wb')
# Download the file a chunk at a time
# Each chunk is sent to handleDownload
# We append the chunk to the file and then print a '.' for progress
# RETR is an FTP command
print 'Getting ' + filename
ftp.retrbinary('RETR ' + filename, handleDownload)
# Clean up the file
print 'Closing file ' + filename
file.close()
You will now fine that it connects and downloads one, and only one file, the last one in the list, writers.list.gz.
Can someone PLEASE tell me what I am doing wrong? I want to cycle through the entire list and have the above for loop download each file, but I just cannot figure it out.
Thanks in advance as I really appreciate any help on this.
Regards,
Jeff

New Topic/Question
Reply



MultiQuote






|