Chapter 17 ■ Ftp
327
When you run this program, you will see output like this:
$ python nlst.py
13 entries:
INDEX
README
ephem_4.28.tar.Z
hawaii_scope
incoming
jupitor-moons.shar.Z
lunar.c.Z
lunisolar.shar.Z
moon.shar.Z
planetary
sat-track.tar.Z
stars.tar.Z
xephem.tar.Z
If you were to use an FTP client to log in to the server manually, you would see the same files listed. The result
will be different when you try another file listing command, as shown in Listing 17-8.
Listing 17-8. Getting a Fancy Directory Listing
#!/usr/bin/env python3
Foundations of Python Network Programming, Third Edition
https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter17/dir.py
from ftplib import FTP
def main():
ftp = FTP('ftp.ibiblio.org')
ftp.login()
ftp.cwd('/pub/academic/astronomy/')
entries = []
ftp.dir(entries.append)
ftp.quit()
print(len(entries), "entries:")
for entry in entries:
print(entry)
if name == 'main':
main()