By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,118 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

How to get file name on a remote server with ftplib?

P: n/a
Hello,

My script is trying to get a file from a remote server, every day it
ftps from a directory. My code works perfect if I know the name of the
file in the remote directory.

ftp.retrbinary('RETR ' + filename, handleDownload)

The problem is that in the future the name of the file is going to be
aleatory. How can I get the file's name (only one file is in that
directory) and store it in a variable before executing ftp.retrbinary?

Thanks a lot for your help,

Alex

Jan 5 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a

alex wrote:
Hello,

My script is trying to get a file from a remote server, every day it
ftps from a directory. My code works perfect if I know the name of the
file in the remote directory.

ftp.retrbinary('RETR ' + filename, handleDownload)

The problem is that in the future the name of the file is going to be
aleatory. How can I get the file's name (only one file is in that
directory) and store it in a variable before executing ftp.retrbinary?

Thanks a lot for your help,

Alex
You can use the nlst(dirname) method to retrieve a directory listing on
the remote server. If the remote server doesn't support NLST command
rhen you could do ftp.retrlines('LIST') and parse the results to get a
directory listing.

Jan 5 '07 #2

P: n/a
On 2007-01-05 16:10, wi******@hotmail.com wrote:
alex wrote:
>My script is trying to get a file from a remote server, every day it
ftps from a directory. My code works perfect if I know the name of the
file in the remote directory.

ftp.retrbinary('RETR ' + filename, handleDownload)

The problem is that in the future the name of the file is going to be
aleatory. How can I get the file's name (only one file is in that
directory) and store it in a variable before executing ftp.retrbinary?

Thanks a lot for your help,
You can use the nlst(dirname) method to retrieve a directory listing on
the remote server. If the remote server doesn't support NLST command
rhen you could do ftp.retrlines('LIST') and parse the results to get a
directory listing.
ftputil ( http://ftputil.sschwarzer.net/ ) could help with
this parsing. A session may look like
>>import ftputil
host = ftputil.FTPHost("ftp.domain.com", "user", "password")
file_names = host.listdir(host.curdir)
file_names
['wanted_filename']
>>file_name = file_names[0]
host.download(file_name, file_name, "b")
host.close()
Ok, I admit this isn't a pure-ftplib solution. :-)

Stefan

--
Dr.-Ing. Stefan Schwarzer
SSchwarzer.com - Softwareentwicklung für Technik und Wissenschaft
http://sschwarzer.com
Jan 6 '07 #3

P: n/a
Thanks guys for the help! I used nlst().
The ftputil seems to be very helpfull, but getting a new library in an
organization like the one I work at is big issue. Thanks anyway :)

wi******@hotmail.com wrote:
alex wrote:
Hello,

My script is trying to get a file from a remote server, every day it
ftps from a directory. My code works perfect if I know the name of the
file in the remote directory.

ftp.retrbinary('RETR ' + filename, handleDownload)

The problem is that in the future the name of the file is going to be
aleatory. How can I get the file's name (only one file is in that
directory) and store it in a variable before executing ftp.retrbinary?

Thanks a lot for your help,

Alex

You can use the nlst(dirname) method to retrieve a directory listing on
the remote server. If the remote server doesn't support NLST command
rhen you could do ftp.retrlines('LIST') and parse the results to get a
directory listing.
Jan 8 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.