Automating FTP file transfers

Matt Goodall matt at pollenation.net
Wed Nov 12 09:17:05 EST 2003


Limey Drink wrote:

>Hi all,
>
>Firstly :-) , is there any where I can search through archived newsgroup
>posts so I am not cluttering up the newsgroup with repeated queries ?
>  
>
Yep, info on all the python lists, including archives, are available here:

     http://python.org/community/lists.html

>And secondly :-), I know this has probably been discussed before but.
>
>I am wanting to do some scripting to automate a few administration tasks,
>one of the first tasks is automating FTP file transfers.
>
>I would like to know if the following could be made more robust using python
>and its FTP libraries rather than executing native OS commands in a shell
>script.
>
>Basically I need to...
>1.  Check on the local system for new files in a specific directory
>If new files exist then...
>2.  Connect to a remote FTP server.
>3.  Transfer local files to a specific directory on the remote FTP server.
>4.  Then remove/archive local files and end session.
>  
>
Most of this should be fairly straightforward. (1) and (4) should be 
covered using the os and os.path modules; for (2) and (3) you need the 
ftplib module.

The only problem you are likely to encounter is getting a remote 
directory listing. The FTP protocol neglected to formalise the response 
to FTP's LIST command and different servers send different results back 
:(. If this is a problem I would recommend using one of the ftpparse 
Python APIs:

    * http://effbot.org/downloads/#ftpparse
    * http://c0re.23.nu/c0de/ftpparsemodule/ftpparse.html

>This is critical operation and though while in the past I have written DOS
>scripts etc. to do simple tasks I have not needed to check for errors as
>they weren't absolutely critical tasks.  I am going to need to run this
>overnight using AT or CRON then I need to know about file transfer errors
>etc. and other problems that f I were actually performing the FTP transfer
>manually I would get feedback of the problem and could re-try the operation.
>  
>
The ftplib module reports errors using the Python exception mechanism so 
you can make it as robust as you need and take whatever action is 
appropriate on errors.

Hope this helps.

Cheers, Matt

-- 
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenation.net
e: matt at pollenation.net







More information about the Python-list mailing list