I would like to list all directories on a ftp directory and then enter each of them. The problem is that my code also list file and try to enter them as well.
Here is the code I am using now:
from ftplib import FTP
ftp = FTP('ftp.overtherainbow.com')
ftp.login()
for name in ftp.nlst():
print "listing: " + name
ftp.cwd(name)
ftp.retrlines('LIST')
ftp.cwd('../')
The FTP protocol doesn't have a way of distinguishing between directories and files (as for listing). I think the best bet would be either, try and fail
try:
ftp.cwd(name)
except ftplib.error_perm as detail:
print("It's probably not a directory:", detail)
Or otherwise you may want to parse the output from the directory listing. This will not be platform independent though, because directory listings differ from OS to OS. As shown here...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With