最近使用python下载FTP的数据,测试数据一直是可行的,但覆盖用例不够,突然测试了一个中文名字的文件就下载失败,提示如下:
ftp.retrbinary 'latin-1' codec can't encode characters in position 13-14: ordinal not in range(256)
通过**ftp.retrlines('LIST')**查看当前看见目录列表时,发现中文都是乱码的。
代码如下
``````
from ftplib import FTPif __name__ == "__main__":
ftp = FTP()
ftp_file_path = "/public/ciba_data_1_train.rar"
host = ''
try:
ftp.connect(host, 21)
ftp.login("<username>", "<password>")
except:
raise IOError('\n FTP login failed!!!')
else:
try:
bufsize = 1024000
local_file_name = '%s_train.rar'%host
with open("/opt/%s"%local_file_name, 'wb') as fid:
ftp.retrbinary('RETR {0}'.format(ftp_file_path), fid.write, bufsize)
print("download success")
except Exception as ERR:
print("Failed to download, details:[%s]"%ERR)
finally:
ftp.quit()
``````
网上搜了一堆都是修改编码格式,但都未解决点,通过stackoverflow才了解到还可以直接修改ftp的编码格式,特此记录下。[解决链接](https://stackoverflow.com/questions/65630061/unicodeencodeerror-latin-1-codec-cant-encode-characters-in-position-11-12-o)
在FTP对象中直接设置:**ftp.encoding='utf8'**
如下解决了编码问题,文件也顺利下载。