try:
    files = ftp.nlst()
except ftplib.error_perm as resp:
    if str(resp) == '550 No files found':
        print('No files in this directory')
else:
    raise
for f in files:
    pass
Using Python's ftplib to get a directory listing, portably