首页 > 编程语言 > 详细

Python ftp client 处理含有中文的文件名

时间:2015-03-31 22:33:33      阅读:389      评论:0      收藏:0      [点我收藏+]
# -*- 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


Python ftp client 处理含有中文的文件名

原文:http://my.oschina.net/cppblog/blog/394394

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!