首页 > 技术文章 > Python的Ftplib问题:UnicodeEncodeError: 'latin-1' codec can't encode characters的解决方法

ahnucao 原文

ftplib中有一个方法是cwd,用来切换目录,需要传入一个dirname,经过个人测试,该dirname不能含有汉字,会抛出:UnicodeEncodeError: 'latin-1' codec can't encode characters 类型的错误。

在google中搜索,stackflow网站有大量类似的问题和解决方法,常见的是因为utf-8和latin-1无法一一对应,一般将字符串encode为utf-8而后decode为cp1252的方案来解决,但是我按照这种方法来做,却无法解决cwd的错误问题(dirname.encode().decode('cp1252')).我相信利用编码的转化是可以解决的,但是我还没有找到。如果有哪位前辈知道如何解决,望请指导告知,谢谢!

而后,利用了一个笨的方法去改变,即:首先正常的ftp.login,然后 利用retrlines('LIST') 方法列出目录,手动复制汉字编码转换后的目录,而后将其作为dirname的值传入到cwd中,由此完成切换目录的工作。如果有人遇到同样的问题,也许可以尝试。

from ftplib import FTP
from os.path import exists
from getpass import getpass
#getpass是弹出一个输入密码的窗口

def getfile(file,site,dirname,usr=(),*,verbose = True,refetch = False):
    if exists(file) and not refetch:
        if verbose:print(file,' already feteched.')
    else:
        if verbose:print('Downloading... ',file)
        local = open(file,'wb')
        try:
            ftp = FTP(site)
            ftp.login(*user)            
            ftp.cwd(dirname)  
            print('Current directory:',ftp.pwd())
            ftp.retrlines('LIST')            
            ftp.retrbinary('RETR '+file,local.write,1024)
            ftp.quit()
        finally:
            local.close()
        if verbose: print('Download Done!')
            
if __name__=='__main__':    

    file = 'myhongze.jpg'
    dirname = './ÏîÄ¿×é³ÉԱ˽ÈË¿Õ¼ä/zgcao/test-python/'
    site = '************'
    user = ('zhigang',getpass('Input Pwd:'))    
    getfile(file,site,dirname,user,refetch=True)
    #在dirnmae直接输入汉字的时候会出现问题,会爆出“UnicodeEncodeError: 'latin-1' codec can't encode characters”的错误,这个查询主要是编码的问题,decode为cp1252和latin-1都
    #无法解决,最后利用ftp.retrlines('LIST')列出汉字的目录,然后复制附录,最后进行处理。

推荐阅读