使用 Paramiko 进行目录传输
如何使用Paramiko传输完整的目录? 我正在尝试使用:
sftp.put("/Folder1","/Folder2")
这给了我这个错误 -
错误:[Errno 21] 是一个目录
How do you use Paramiko to transfer complete directories?
I'm trying to use:
sftp.put("/Folder1","/Folder2")
which is giving me this error -
Error : [Errno 21] Is a directory
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您可以子类化 paramiko.SFTPClient 并向其添加以下方法:
要使用它:
You can subclass paramiko.SFTPClient and add the following method to it:
To use it:
您需要像在本地使用 python 一样执行此操作(如果您没有使用 Shutils)。
将
os.walk()
与sftp.mkdir()
和sftp.put()
结合起来。您可能还需要使用 os.path.islink() 检查每个文件和目录,具体取决于您是否要解析符号链接。You'll need to do this just like you would locally with python (if you weren't using shutils).
Combine
os.walk()
, withsftp.mkdir()
andsftp.put()
. You may also want to check each file and directory withos.path.islink()
depending on whether you want to resolve symlinks or not.这是我的代码:
Here's my piece of code:
只需使用 paramiko 即可轻松完成这一切。
以下代码的高级摘要是:
- 连接到 SFTP(步骤 1 到 3)
- 指定您的源文件夹和目标文件夹。 (步骤 4)
- 将它们一一复制到您喜欢的任何地方(我已将它们发送到/tmp/)。 (步骤5)
This can all be done quite easily using just paramiko.
A high level summary of the code below is:
- connect to the SFTP (steps 1 to 3)
- specify your source and target folders. (step 4)
- copy them over one by one to wherever you like (I've sent them to /tmp/). (step 5)
适合我做这样的事情,所有文件夹和文件都复制到远程服务器。
Works for me doing something like this, all folder and files are copied to the remote server.
您可以将
sftp = self.client.open_sftp()
替换为 paramiko 的,并在此处删除libcloud
。You might replace
sftp = self.client.open_sftp()
with paramiko's one and get rid oflibcloud
here.我认为你做不到。查找 os.walk 的文档并“手动”复制每个文件。
I don't think you can do that. Look up the documentation for
os.walk
and copy each file "manually".这是我在 StackOverflow 上的第一个回答。我今天有一个与此类似的任务。因此,我尝试找到一种使用 python 和 paramiko 将整个文件夹从 Windows 复制到 Linux 的直接方法。经过一番研究,我想出了这个解决方案,适用于其中包含子文件夹和文件的较小尺寸的文件夹。
该解决方案首先为当前文件夹制作 zip 文件(os.walk() 在这里非常有帮助),然后复制到目标服务器并在那里解压缩。
This is my first StackOverflow answer. I had a task today which is similar to this. So, I tried to find a direct way to copy entire folder from windows to linux using python and paramiko. After a little research, I came up with this solution which works for smaller size folders with subfolders and files in it.
This solution first makes the zip file for the current folder (os.walk() is very much helpful here), then copies to destination server and unzip there.
Paramiko 本身不支持目录传输。正如此处许多现有答案所示,您必须实施它。
您可以检查 pysftp 代码。它是 Paramiko 的包装器,支持递归操作。请参阅
pysftp.Connection.put_r()< /code>
pysftp。 Connection.get_r()
我不建议直接使用pysftp代码(pysftp vs. Paramiko )但是,因为它不再维护并且有一些错误。对于基于 pysftp 的独立可移植仅 Paramiko 代码,请参阅我的答案:
Paramiko does not support directory transfers on its own. You have to implement it, as many existing answers here show.
You can check pysftp code. It's a wrapper around Paramiko that supports recursive operations. See
pysftp.Connection.put_r()
pysftp.Connection.get_r()
I do not recommend using pysftp code directly (pysftp vs. Paramiko) though, as it is not maintained anymore and has some bugs. For working standalone portable Paramiko-only code based on pysftp see my answers to:
我的回答和上面类似,只是列一个清单,然后一项一项地转移。
my answer is similar with above just make a list, and then transfer one by one.
这是我的方法,但是代码也处理隐藏文件
This is my approach but and the code handle hidden files also
根据 skoll 投票最高的答案,我提出了一个可能更高级别的解决方案(递归是一个选项;有返回值;如果是一个文件可以是目录或文件名)并且现代(类型提示;pathlib):
Based on the top voted answer by skoll, I made a solution that may be more high-level (recursive is an option; has return values; if <local_path> is a file <remote_path> can be a directory or filename) and modern (type hints; pathlib):
据我所知,Paramiko 不支持递归文件上传。但是,我发现了 此处使用 Paramiko 进行递归上传的解决方案。以下是他们的递归上传函数的摘录:
您可以尝试使用他们的函数
SCPClient.put
调用上述函数进行递归上传,或者自行实现。As far as I know, Paramiko does not support recursive file upload. However, I have found a solution for recursive upload using Paramiko here. Follows an excerpt of their recursive upload function:
You may try to either use their function
SCPClient.put
invoking the above function for recursive upload or implement it on your own.如果您想对每个文件夹进行并行复制,可以使用(请记住,它将忽略本地已存在的文件):
If you would like to have parallel copy per folder you can use (keep in mind that it will ignore files that already exists localy):