ftp: get list of files

E

eels

Hello,

I want to get a listing of my files at web server with ftp. I wantn 't
download the files.
With yyy = ftp.retrlines('LIST') I get this listing at stdout, but I
need this information at variable yyy.
How can I resolve this problem?

Thank's for your hints, Thomas
 
L

Lawrence Oluyede

eels said:
With yyy = ftp.retrlines('LIST') I get this listing at stdout, but I
need this information at variable yyy.
How can I resolve this problem?

As written in the doc retrlines has an optional parameter (a callback function)
called on each line retrieved, so you can do something like this:

from ftplib import FTP

def writeline(data):
fd.write(data + "\n")

f = FTP('ftp.kernel.org')
f.login()

f.cwd('/pub/linux/kernel')
fd = open('README', 'wt')
f.retrlines('RETR README', writeline)
fd.close()
f.quit()
 
F

Fredrik Lundh

eels said:
I want to get a listing of my files at web server with ftp. I wantn 't
download the files.
With yyy = ftp.retrlines('LIST') I get this listing at stdout, but I
need this information at variable yyy.
How can I resolve this problem?

as mentioned in the documentation, you need to provide a callback if
you want to collect the data from a retr operation.

yyy = []
ftp.retrlines("LIST", yyy.append)

should do the trick.

(also see the dir and nlst methods)

</F>
 
E

eels

Hello Fredrik,

thank's for your quick help.
Greetings Eels

Fredrik said:
eels said:
I want to get a listing of my files at web server with ftp. I wantn 't
download the files.
With yyy = ftp.retrlines('LIST') I get this listing at stdout, but I
need this information at variable yyy.
How can I resolve this problem?

as mentioned in the documentation, you need to provide a callback if
you want to collect the data from a retr operation.

yyy = []
ftp.retrlines("LIST", yyy.append)

should do the trick.

(also see the dir and nlst methods)

</F>
 
P

Paul Watson

Lawrence said:
As written in the doc retrlines has an optional parameter (a callback function)
called on each line retrieved, so you can do something like this:

from ftplib import FTP

def writeline(data):
fd.write(data + "\n")

f = FTP('ftp.kernel.org')
f.login()

f.cwd('/pub/linux/kernel')
fd = open('README', 'wt')
f.retrlines('RETR README', writeline)
fd.close()
f.quit()

How about a little more portability?

def writeline(data):
fd.write(data + os.linesep)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top