Paramiko,sftp无法从linux服务器下载到windows

时间:2014-06-04 14:14:30

标签: python linux windows sftp paramiko

我尝试将.zip文件从linux机器下载到Windows机器(本地)。我确信我的路径和文件很好,因为它与我的Linux机器完美配合。所以,我得到了Windows:

Traceback (most recent call last)

   self.sftp.get(self.fullremotepath, localpath)
   File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 638, in get file_size = self.stat(remotepath).st_size
   File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 331, in statt, msg = self._request(CMD_STAT, path)
   File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 649, in _request return self._read_response(num)
   File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 696, in_read_response self._convert_status(msg)
   File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 722, in_convert_status
   raise IOError(errno.ENOENT, text) IOError: [Errno 2] No such file

这是我的剧本:

class DownloadFiles(object):

    def __init__(self, path, missing):

    '''
    path    : local path to the month directory 
    missing : list of the missing files for a month

    '''

    host = "xxx"                    #hard-coded
    port = 22
    transport = paramiko.Transport((host, port))

    password = "xx"                #hard-coded
    username = "guest"                #hard-coded
    transport.connect(username = username, password = password)

    self.sftp = paramiko.SFTPClient.from_transport(transport)

    self.roothremotepath = os.path.join(os.sep,'data','project','archive','data')
    self.download(path, missing)

    self.sftp.close()
    transport.close()


def download(self, path, missing):

    for i in missing:
        yearMonth  = os.path.basename(str(path))
        self.fullremotepath = os.path.join(self.roothremotepath, yearMonth, i + '.zip')
        localpath  = os.path.join(str(path), i + '.zip')
        self.sftp.get(self.fullremotepath, localpath)
        self.unzip(localpath)

def unzip(self, zippath):

    with zipfile.ZipFile(zippath, "r") as z:
        z.extractall(zippath.replace('.zip','')) 

1 个答案:

答案 0 :(得分:1)

我认为这一行是你的问题:

self.roothremotepath = os.path.join(os.sep,'data','project','archive','data')

os.sep的返回值取决于您的主机操作系统。

在Linux上运行:

>>> import os
>>> rootpath = os.path.join(os.sep, 'data', 'project', 'archive', 'data')
>>> print rootpath
/data/project/archive/data

在Windows上运行:

>>> import os
>>> rootpath = os.path.join(os.sep, 'data', 'project', 'archive', 'data')
>>> print rootpath
\data\project\archive\data

您可以尝试在远程路径中硬编码UNIX样式的路径分隔符'/'