我尝试将 .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',''))
最佳答案
我认为这一行是你的问题:
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 风格的路径分隔符 '/'
。
关于python - Paramiko,sftp 无法从 linux 服务器下载到 windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24039873/