Eliminating an element in a for loop

I am trying to find the total size of the root directory on an FTP server. However, I don't have access to one of the directories in the root.

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

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

      

This generates an error:

ftplib.error_perm: 550 Could not get file size.

      

I cannot find any documentation on excluding an element from a for loop.

+3


source to share


1 answer


Just catch the exception and pass

or continue

, for example:



for filename in ftp.nlst("."):
    try:
        ftp.cwd(filename)
        size += ftp.size(".")    
    except ftplib.error_perm:
        pass
print(size)

      

+2


source







All Articles