Are the error_perm and msg in "except error_perm, msg" special?
I'm reading this script. It synchronizes files via FTP.
I'm confused with this statement in line 106
try:
...
except error_perm, msg:
...
It seems the variables error_perm开发者_Go百科 and msg come from nowhere. When the try part goes wrong, the script halts and fails to go into the except part.
You have the next import on the top of the file:
from ftplib import FTP, error_perm
error_perm
is an error class.
statement
except error_perm, msg:
catches any exception with type error_perm
and stores exception object in variable msg
.
import is correct, exception handling is a bit different syntax for python 3.6 and up:
except error_perm as msg:
print(f"FTP error: {msg}",flush=True)
精彩评论