3

I am trying to find the total size of a root directory on an FTP server. However, I do not have access to one of the directories in the root.

I want to use this function to sum the sizes of the directories in the root:

size = 0
for filename in ftp.nlst("."):
    ftp.cwd(filename)
    size += ftp.size(".")    
print(size)

This generates the error:

ftplib.error_perm: 550 Could not get file size.

I can't find any documentation about excluding an item from a for loop.

1 Answers1

2

Just catch the exception and pass or continue, e.g.:

for filename in ftp.nlst("."):
    try:
        ftp.cwd(filename)
        size += ftp.size(".")    
    except ftplib.error_perm:
        pass
print(size)
AChampion
  • 29,683
  • 4
  • 59
  • 75