简体   繁体   中英

pysftp copies file but its empty

I am having an issue copying files with pysftp. This was originally a WinSCP script that worked. I can copy the file manually using the credentials supplied in the script below in winscp.com and filezilla. Ive verified that the credentials are correct and both the remote and local paths exist. the file looks like it copies ( I can see file in local directory), but it is empty. Below is the error. The same script works when connecting and downloading files from other hosts.

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]

this is my code:

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)

Why don't you try getfo(remotepath, flo, callback=None) as such:

sftp.getfo (rpath + item, open(lpath + item, 'wb'))

See this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM