You can access error reponse string using <exception_obj>.args[0]
. It contains strings like '550 /no-such-dir: No such file or directory'
.
To get error code (only leading three chracters), use <exception_obj>.args[0][:3]
.
For example:
import ftplib
ftp = ftplib.FTP('ftp.hq.nasa.gov')
ftp.login('anonymous', 'user@example.com')
try:
ftp.cwd('/no-such-dir')
except ftplib.error_perm as e:
print('Error {}'.format(e.args[0][:3]))
finally:
ftp.quit()