Python ftp client 處理含有中文的文件名
# -- 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</pre>
本文由用戶 dwd4 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!