Python ftp client 处理含有中文的文件名
- 作者: 此人已葬终南山
- 来源: 51数据库
- 2022-08-12
# -*- 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
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
