Python ftp clientは中国語を含むファイル名を処理する

1066 ワード

# -*- coding:utf-8 -*-
'''
ftplib      

             :
    utf-8      ,        unicode              。
    :
Server: File Zilla Server 0.9.50 
Client OS: Win7

'''
import ftplib
from ftplib import FTP
ftp = ftplib.FTP( )
ftp.connect(host='localhost',port = 21,timeout=5)     # connect to host, default port
ftp.login(user='honglei',passwd='111111' )

#names =[]
#def mycall(line):
    #a = [item for item in line.split(" ") if item!=" "]
    #name = a[-1]#.decode('utf-8')
    #size = a[-5] 
    #names.append(name)
#ftp.retrlines('LIST',callback=mycall)           # list directory contents
filename = '\xe8\xbf\x99\xe6\x98\xaf\xe4\xb8\xad\xe6\x96\x87.txt' #   LIST    
ftp.retrbinary('RETR '+ filename, open(filename.decode('utf-8'), 'wb').write)

filename = u"          .txt"
try:
    ftp.storbinary('STOR '+ filename.encode('utf-8'), open(filename, 'rb'))
except ftplib.error_perm as e:
    pass