gpt4 book ai didi

Python 错误捕获和 FTP

转载 作者:太空狗 更新时间:2023-10-29 22:02:05 24 4
gpt4 key购买 nike

尝试获取 Python 中 FTP 库的句柄。 :)

到此为止。

from ftplib import FTP

server = '127.0.0.1'
port = '57422'

print 'FTP Client (' + server + ') port: ' + port

try:
ftp = FTP()
ftp.connect(server, port, 3)
print 'Connected! Welcome msg is \"' + ftp.getwelcome() + '\"'

ftp.cwd('\\')

x = '1'
currentDir = ''

except: //***What do I put here?***

http://docs.python.org/library/ftplib.html说有几个错误代码我可以捕获但我做不到

除了:ftplib.all_errors

第二个问题。 :P如何检索有关错误的更多具体信息?也许是错误代码?

对 python 非常陌生(一个小时左右)。

最佳答案

I can't do

except: ftplib.all_errors

当然不是,那只是错误的语法!但是当然你可以用正确的语法来做到这一点:

except ftplib.all_errors:

即异常元组之后的冒号。

How can I retrieve more specific information on the error? Perhaps the error code?

except ftplib.all_errors as e:
errorcode_string = str(e).split(None, 1)[0]

例如,当完整的错误消息是 “530 Login authentication failed” 时,'530' 现在将是 errorcode_string 的值。

您可以在 docs 中找到其余的异常.

关于Python 错误捕获和 FTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3169725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com