using python ftp

MRAB python at mrabarnett.plus.com
Wed Dec 22 21:24:09 EST 2010


On 23/12/2010 02:12, Anurag Chourasia wrote:
> Hi Matt,
>
> I have a snippet to "upload" files (that match a particular search
> pattern) to a remote server.
>
> Variable names are self explanatory. You could tweak this a little to
> "download" files instead.
>
> from ftplib import FTP
> ftp = FTP(hostname)
> ftp.login(user_id,passwd)
> ftp.cwd(remote_directory)
> files_list=glob.glob(file_search_pattern)
> for file in files_list:
>      try:
>          ftp.storlines('STOR ' + file, open(file))

You should open the file in binary mode:

     ftp.storlines('STOR ' + file, open(file, 'rb'))

This isn't necessary on *nix, but it's recommended for portability.

>      except Exception, e:
>          print ('Failed to FTP file: %s' %(file))
> ftp.close()
>
[snip]



More information about the Python-list mailing list