[ftputil] Newbie FTP question

Stefan Schwarzer sschwarzer at sschwarzer.net
Wed Nov 28 19:16:28 CET 2007


Hi Chad,

On 2007-11-28 18:36, Chad Moore wrote:
> I am wondering how to simply download an entire directory consisting of
> subdirectories and files.  I am sure I can figure out how to "walk"
> through the directory structure, but isn't there an easier way to just
> suck down an entire direcotry from the FTP site?

There's some code for copying an entire directory tree in the
ftputil source code repository:

http://ftputil.sschwarzer.net/trac/browser/trunk/ftp_sync.py

See

http://ftputil.sschwarzer.net/trac/browser/trunk/_test_ftp_sync.py

for the usage semantics. Due to the design, you can use the
code to copy trees from local to local, local to remote, remote
to local and remote to remote.

As the name of the module implies, it's intended to become a syncing
library, i. e. later it should only copy the files which aren't
present in the target directory or have an older date/time. This
would resolve issue #6 (http://ftputil.sschwarzer.net/trac/ticket/6).
However, I'm very busy at the moment and haven't worked on the
code for some time. Sorry.

> Here is my code and
> details.

My unqualified comments on your code follow. :-)

> - - - - Description - - - -
> Log into FTP
> If a folder with todays date exists, download the entire directory to a
> local drectory
> If it doesnt exist return "nothing ready today"
> 
> - - - - Error - - - -
> ftputil.ftp_error.FTPIOError: 550 2007_11_28/: Not a regular file
> 
> - - - - Code - - - -
> 
> import ftputil
> import os
> import sys
> from time import strftime
> from nt import mkdir

Why don't you use mkdir from the os module? This should wrap
platform differences.

> localPath = "C:/Documents and Settings/USERNAME/My Documents/Downloads"
> 
> #ftp host, login and password
> host = ftputil.FTPHost(' ftp.host.com <http://ftp.host.com>', 'user',
> 'pass')

This host argument won't work, probably you didn't use something
like "host <http://...>" but just "ftp.host.com"?

> #host.set_directory_format("unix")

Explicitly setting the directory format is deprecated with newer
versions of ftputil. You should set the parser if necessary (see
the documentation on the website). Which version of ftputil do
you use?

> #directories on the FTP site
> filesToDownloadFolder = 'subfolder01/subfolder02'
> host.chdir(filesToDownloadFolder)
> 
> #get todays date
> todaysDate = strftime("%Y_%m_%d/")
> ##if folder exists, download it and send email notification.  if not,
> send email, "nothing ready for today"
> 
> host.download(todaysDate, localPath, 'b')  # remote, local, binary mode

Unfortunately, the download method only works on files and links
to them, not on directories.

Best regards,
Stefan


More information about the ftputil mailing list