开发者

Are the error_perm and msg in "except error_perm, msg" special?

开发者 https://www.devze.com 2023-03-28 13:26 出处:网络
I\'m reading this script.It synchronizes files via FTP. I\'m confused with this statement in line 106 try:

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消