需要通过 sftp 方式下载和上传文件,找到了 python 第三方 sftp 库 paramkio,但是发现对中文支持不好
之前碰到一个中文名文件显示问题
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
aa = sftp_test.listdir()
File "C:\ProgramData\Anaconda3\lib\site-packages\paramiko\
sftp_client.py", line 218, in listdir
return [f.filename for f in self.listdir_attr(path)]
File "C:\ProgramData\Anaconda3\lib\site-packages\paramiko\
sftp_client.py", line 254, in listdir_attr
filename = msg.get_text()
File "C:\ProgramData\Anaconda3\lib\site-packages\paramiko\
message.py", line 178, in get_text
return u(self.get_string())
File "C:\ProgramData\Anaconda3\lib\site-packages\paramiko\
py3compat.py", line 156, in u
return s.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcd in position 0: invalid continuation byte
网上找了下,发现原因是 paramkio 把编码写死了,全部都是 utf-8,
https://www.cnblogs.com/kwang-cai/articles/10611946.html修改源码里的
py3compat.py 代码解决了,
和网上不同的是,我改成 gb2312,而不是文中的 ISO-8859-1,(因为改成 ISO-8859-1 还是会有乱码,不知道为什么)
然后碰到第二个问题,
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
sftp_test.get('邮寄清单 20190816 日卡.zip', r'D:\App\ICcard_trans\邮寄清单 20190816 日卡.zip')
File "C:\ProgramData\Anaconda3\lib\site-packages\paramiko\
sftp_client.py", line 802, in get
size = self.getfo(remotepath, fl, callback)
File "C:\ProgramData\Anaconda3\lib\site-packages\paramiko\
sftp_client.py", line 778, in getfo
file_size = self.stat(remotepath).st_size
File "C:\ProgramData\Anaconda3\lib\site-packages\paramiko\
sftp_client.py", line 493, in stat
t, msg = self._request(CMD_STAT, path)
File "C:\ProgramData\Anaconda3\lib\site-packages\paramiko\
sftp_client.py", line 813, in _request
return self._read_response(num)
File "C:\ProgramData\Anaconda3\lib\site-packages\paramiko\
sftp_client.py", line 865, in _read_response
self._convert_status(msg)
File "C:\ProgramData\Anaconda3\lib\site-packages\paramiko\
sftp_client.py", line 894, in _convert_status
raise IOError(errno.ENOENT, text)
FileNotFoundError: [Errno 2] File does not exist
通过 get 方法下载带中文名文件时报文件不存在的错,但是下载英文名的文件都是正常的。这次网上真找不到了。。
求解决方案。。