我在使用 pysftp 复制文件时遇到问题。这最初是一个有效的 WinSCP 脚本。我可以使用下面 winscp.com 和 filezilla 中脚本中提供的凭据手动复制文件。我已验证凭据正确,并且远程和本地路径都存在。该文件看起来像是复制的(我可以在本地目录中看到文件),但它是空的。下面是错误。从其他主机连接和下载文件时,相同的脚本也有效。
Traceback (most recent call last):
File "C:\Users\myuser\pythonprograms\Download.py", line 28, in <module>
main()
File "C:\Users\myuser\pythonprograms\Download.py", line 25, in main
sftp_download()
File "C:\Users\myuser\pythonprograms\Download.py", line 21, in sftp_download
sftp.get(rpath + item, lpath + item, preserve_mtime=True)
File "C:\Python27\lib\site-packages\pysftp.py", line 233, in get
self._sftp.get(remotepath, localpath, callback=callback)
File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 720, in get
size = self.getfo(remotepath, fl, callback)
File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 689, in getfo
file_size = self.stat(remotepath).st_size
File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 413, in stat
t, msg = self._request(CMD_STAT, path)
File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 729, in _request
return self._read_response(num)
File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 776, in _read_response
self._convert_status(msg)
File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 804, in _convert_status
raise IOError(errno.EACCES, text)
IOError: [Errno 13] Permission denied
[Finished in 2.3s with exit code 1]
这是我的代码:
import pysftp
from datetime import date
from datetime import timedelta
lpath = 'C:/local/path/in/'
rpath = '/remote/path/out/'
yymd = (date.today() - timedelta(days=1)).strftime('%Y%m%d')
with pysftp.Connection('host', username='u', password='p') as sftp:
with sftp.cd():
sftp.chdir(rpath)
for item in sftp.listdir():
if yymd + '.txt' in item:
sftp.get(rpath + item, lpath + item, preserve_mtime=True)