2015-05-07 49 views
1

我有問題複製文件與pysftp。這原本是一個有效的WinSCP腳本。我可以使用winscp.com和filezilla中腳本中提供的憑據手動複製文件。我證實了憑據是正確的,並且存在遠程和本地路徑。該文件看起來像它複製(我可以在本地目錄中看到文件),但它是空的。以下是錯誤。從其他主機連接和下載文件時,該腳本可以工作。pysftp複製文件,但其空

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) 

回答

1

你爲什麼不嘗試getfo(remotepath, flo, callback=None)這樣:

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

this