0

I'm testing a FTP script in Python 3 to list the files in a directory path give, but when I'm trying with try/except handlers, I'm getting a syntax error.

Below is the script code. Please advise what I'm missing or doing wrong.

#!/usr/bin/python env
import ftplib
def FtpMirroList():
  ftp = ftplib.FTP("ftp.example.com")
  ftp.login("lodgy", "pass123")
  ftp.cwd("/my/research/folder")

  files = []
  try:
    files = ftp.nlst()
  except ftplib.error_perm, resp:
    if str(resp) == "550 No files found":
      printi("No files in this directory")
    else:
      raise
  for f in files:
    printi(f)

FtpMirroList()

It's raising the below error:

  File "./ftplib-example-2.py", line 12
    except ftplib.error_perm, resp:
                            ^
SyntaxError: invalid syntax
4
  • 3
    except ftplib.error_perm as resp:? (as instead of , ) Commented Jan 9, 2018 at 15:42
  • @pycoder you correctly catched it ..thnx Commented Jan 9, 2018 at 15:47
  • Python3 changed the exception syntax but the Python3 syntax can be used in Python2. As pycoder wrote use as instad the comma. If you wanto multiple exceptions to handle then use except (exc1, exc2, exc3) as e:. Commented Jan 9, 2018 at 15:47
  • @GáborFekete .. Sure i got it know , currently reading the python 3 error handling..thnx Commented Jan 9, 2018 at 15:49

1 Answer 1

2

#!/usr/bin/python env should be #!/usr/bin/env python

except ftplib.error_perm, resp This synthax is not supported in python3. Replace comma with as.

Sign up to request clarification or add additional context in comments.

2 Comments

that's correct .thnx
@Karn if it helped you please mark answer as accepted so that the question can be marked as resolved.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.