標籤:
# -*- coding:utf-8 -*-‘‘‘ftplib庫的讀寫操作檔案名稱中含有中文的特殊處理:收到的為utf-8格式的字串,在儲存時需要使用unicode編碼的檔案名稱寫入本地檔案系統。測試環境:Server: File Zilla Server 0.9.50 Client OS: Win7‘‘‘import ftplibfrom ftplib import FTPftp = ftplib.FTP( )ftp.connect(host=‘localhost‘,port = 21,timeout=5) # connect to host, default portftp.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 contentsfilename = ‘\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
Python ftp client 處理含有中文的檔案名稱