Python ftp client 处理含有中文的文件名
2018-07-20 来源:open-open
# -*- 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
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:iOS实现旋转
最新资讯
热门推荐