>>> f.dir(L.append) # ditto for detailed list
>>> for x in L[:3]: print(x)
...
-rw-r--r-- 1 ftp ftp 8173 Mar 19 2006 2004-longmont-classes.html
-rw-r--r-- 1 ftp ftp 9739 Mar 19 2006 2005-longmont-classes.html
-rw-r--r-- 1 ftp ftp 805 Jul 8 2006 2006-longmont-classes.html
On the other hand, the server I’m using in this section does include the special dot
names; to be robust, our scripts must skip over these names in remote directory listings
just in case they’re run against a server that includes them (here, the test is required to
avoid falling into an infinite recursive loop!). We don’t need to care about local directory
listings because Python’s os.listdir never includes “.” or “..” in its result, but things
are not quite so consistent in the “Wild West” that is the Internet today:
>>> f = FTP('learning-python.com')
>>> f.login('lutz', 'xxxxxxxx') # output lines omitted
>>> for x in f.nlst()[:5]: print(x) # includes. and .. here
...
.
..
.hcc.thumbs
2009-public-classes.htm
2010-public-classes.html
>>> L = []
>>> f.dir(L.append) # ditto for detailed list
>>> for x in L[:5]: print(x)
...
drwx---r-x 19 5693094 450 8192 May 4 10:59.
drwx---r-x 19 5693094 450 8192 May 4 10:59 ..
drwx------ 2 5693094 450 4096 Feb 18 05:38 .hcc.thumbs
-rw----r-- 1 5693094 450 15824 May 1 14:39 2009-public-classes.htm
-rw----r-- 1 5693094 450 18083 May 4 09:05 2010-public-classes.html
The output of our clean-all script in action follows; it shows up in the system console
window where the script is run. You might be able to achieve the same effect with a
“rm –rf” Unix shell command in a SSH or Telnet window on some servers, but the
Python script runs on the client and requires no other remote access than basic FTP on
the client:
C:\PP4E\Internet\Ftp\Mirror> cleanall.py
Password for lutz on learning-python.com:
connecting...
file 2009-public-classes.htm
file 2010-public-classes.html
file Learning-Python-interview.doc
file Python-registration-form-010.pdf
file PythonPoweredSmall.gif
directory _derived
file 2009-public-classes.htm_cmp_DeepBlue100_vbtn.gif
file 2009-public-classes.htm_cmp_DeepBlue100_vbtn_p.gif
file 2010-public-classes.html_cmp_DeepBlue100_vbtn_p.gif
file 2010-public-classes.html_cmp_deepblue100_vbtn.gif
directory _vti_cnf
898 | Chapter 13: Client-Side Scripting