我想递归地将文件从 Linux 服务器下载到 Windows 桌面。我正在使用以下代码:
import pysftp
import sys
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(host="192.168.1.1", username="user", password="secret", cnopts=cnopts) as sftp:
sftp.get_r ('test_sftp', 'C:\\Users\\user\\Desktop\\my_dir\\', preserve_mtime=False)
sftp.close()
我遇到了以下错误(递归复制文件时):
Traceback (most recent call last):
File "blah.py", line 12, in <module>
sftp.get_r ('teste_sftp', 'C:\\Users\\user\\Desktop\\my_dir\\', preserve_mtime=False)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\pysftp\__init__.py", line 311, in get_r
preserve_mtime=preserve_mtime)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\pysftp\__init__.py", line 249, in get
self._sftp.get(remotepath, localpath, callback=callback)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\sftp_client.py", line 720, in get
with open(localpath, 'wb') as fl:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\user\\Desktop\\my_dir\\test_sftp/n1/M51'
我知道 pysftp 正在尝试将文件夹结构从源重新创建到目标文件夹,但它在 Windows 机器上工作并且斜杠应该是反斜杠这一事实是不明智的。
如何解决它?