470,821 Members | 1,995 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,821 developers. It's quick & easy.

FTP transfert

Hi,

given this code:
from ftplib import FTP

connection = FTP('ftp.python.org')
connection.login()
connection.dir()
connection.close()

The script write on the standart output a list of files and directories
present on the server. Good. But how can I have this output in a string
that I can work with ? sys.stdout is an open file object, is'nt it ?
So if I do :

string_out = sys.stdout.readlines()

the script of course freeze. (what is for me logic)

How can I solve the problem ?

Why connection.sendcmd('ls') does not work ? the same for
connection.sendcmd('LIST') ? 'ls' is a valid ftp command. In the RFC of
FTP the commands are differents as the one I use every day : no get,
mget, put, mput, ... but LIST, RETR, ... what is the difference ?

Thanks for your help

Antoine

Jul 18 '05 #1
4 1725
Antoine Logean <al@biolinux.ch> writes:
connection = FTP('ftp.python.org')
connection.login()
sys.stdout = open(outfilename, "w")
connection.dir()
connection.close()

Jul 18 '05 #2
Antoine Logean <al@biolinux.ch> writes:
connection.dir()


Check the docs for this method; it can take an argument, a callback
function. You can probably have this function build the representation
you want.
Jul 18 '05 #3
Antoine Logean wrote:
from ftplib import FTP

connection = FTP('ftp.python.org')
connection.login()
connection.dir()
connection.close()

The script write on the standart output a list of files and directories
present on the server. Good. But how can I have this output in a string
that I can work with ? sys.stdout is an open file object, is'nt it ?


Seems like dir() accepts a callback that takes one argument, so you need not
mess with stdout. E. g.:

from ftplib import FTP

connection = FTP('ftp.python.org')
connection.login()
lines = []
connection.dir(lines.append)
connection.close()

print "\n".join(lines)

Peter

Jul 18 '05 #4
Antoine Logean <al@biolinux.ch> writes:
Hi, Why connection.sendcmd('ls') does not work ? the same for
connection.sendcmd('LIST') ? 'ls' is a valid ftp command. In the RFC of
FTP the commands are differents as the one I use every day : no get,
mget, put, mput, ... but LIST, RETR, ... what is the difference ?


The RFC commands constitute the protocol between FTP servers/clients and are
specified (reasonably) exactly to get good interworking between different
implementations. The dir, ls, get etc commands constitute the user interface
and are modelled on what a user on that system might expect to know. The
Python library, unusually for Python, does not do much in the way of hiding
the protocol level commands. Perhaps there are some 3rd party addons to add a
more user friendly level, the vaults of Parnassus
(http://www.vex.net/parnassus/) seems to have some promising libraries.

Eddie
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by David Chaumeil | last post: by
3 posts views Thread by Eric Caron | last post: by
reply views Thread by lukas | last post: by
3 posts views Thread by Alexandre Jaquet | last post: by
reply views Thread by Simon | last post: by
1 post views Thread by a | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.