15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

pwd() Current working directory<br />

cwd(path) Change current working directory to path<br />

dir ([path [,...[,cb]]) Displays directory listing of path; optional callback cb<br />

passed to retrlines()<br />

nlst([path [,...] ) Like dir() but returns a list of filenames instead of<br />

displaying<br />

retrlines(cmd [ ,cb ] ) Download text file given FTP cmd e.g., "RETR<br />

filename"; optional callback cb for processing each<br />

line of file<br />

retrbinary (cmd, cb [ , bs =8192[ ,ra ]] ) Similar to retrlines() except for binary file; callback<br />

cb for processing each block (size bs defaults to 8K)<br />

downloaded required<br />

storlines (cmd, f) Upload text file given FTP cmd e.g., "STOR filename";<br />

open file object f required<br />

storbinary (cmd, f [ , bs= 8192] ) Similar to storlines() but for binary file; open file<br />

object f required, upload blocksize bs defaults to 8K<br />

rename (old, new) Rename remote file from old to new<br />

delete (path) Delete remote file located at path<br />

mkd (directory) Create remote directory<br />

rmd (directory) Remove remote directory<br />

quit() Close connection and quit<br />

The methods you will most likely use in a normal FTP transaction include login(), cwd(), dir(), pwd(),<br />

stor*(), retr*(), and quit(). There are more FTP object methods not listed in the table which you may<br />

find useful. Please see the <strong>Python</strong> documentation for detailed information on FTP objects:<br />

http://python.org/docs/current/lib/ftp-objects.html<br />

17.2.5. Interactive FTP Example<br />

An example of using FTP with <strong>Python</strong> is so simple to use that you do not even have to write a script. You<br />

can just do it all from the interactive interpreter and see the action and output in real time. This is a<br />

sample session we did years ago when there was still an FTP server running at python.org:<br />

>>> from ftplib import FTP<br />

>>> f = FTP('ftp.python.org')<br />

>>> f.login('anonymous', '-help@python.org')<br />

'230 Guest login ok, access restrictions apply.'<br />

>>> f.dir()<br />

total 38<br />

drwxrwxr-x 10 1075 4127 512 May 17 2000 .<br />

drwxrwxr-x 10 1075 4127 512 May 17 2000 ..<br />

drwxr-xr-x 3 root wheel 512 May 19 1998 bin<br />

drwxr-sr-x 3 root 1400 512 Jun 9 1997 dev

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!