I'm having difficulty retrieving a list of directories on a remote FTP site. This answer does not work (I get no output, it appears to hang).
Here's the simplest implementation I can think of - it should just print out the full path of every directory it finds:
import ftplib
def ftp_walk(ftp):
print 'Path:', ftp.pwd()
dirs = ftp.nlst()
for item in (path for path in dirs if path not in ('.', '..')):
try:
ftp.cwd(item)
print 'Changed to', ftp.pwd()
ftp_walk(ftp)
ftp.cwd('..')
except Exception, e:
print item, e
ftp = ftplib.FTP('ftp.site.com')
ftp.login('user', 'pass')
ftp.cwd('1')
ftp_walk(ftp)
My directory structure is:
1/
1-1/
1-2/
1-2/
1-3/
1-4
However, it only outputs the following. It suggests that the working directory is changed once to a new subdirectory, but when ftp_walk() is called with the new cwd, it doesn't go any further:
> Path: 1/
> Changed to 1-1/