Recursive SFTP listdir in Python?

我们两清 提交于 2019-12-01 12:32:53

问题


I need to recursively list the content of a directory that contains a lot of subdirectories (more than 16,000).

I am currently using Paramiko's SFTP client, which doesn't offer any recursive listdir functionality. So I have to first run listdir on the parent folder, and then another listdir for each of the (many, many) subdirectories. It takes too long to run.

Is there any way to run the recursive listdir in a single SFTP call? I'm not limited to the Paramiko package, it's just the package that we're currently using.

I cannot use Paramiko's ssh.exec_command('ls -R <path>') due to local administrative restrictions (it would just return an error message).


回答1:


So I have to first run listdir on the parent folder, and then another listdir for each of the (many, many) subdirectories.

That's the correct approach.

For an example of implementation, see
Python pysftp get_r from Linux works fine on Linux but not on Windows


Is there any way to run the recursive listdir in a single SFTP call? I'm not limited to the Paramiko package, it's just the package that we're currently using.

No. That's not limitation of Paramiko. SFTP protocol itself does not have any way to retrieve recursive listing. Not that other similar protocols (like FTP) have such function commonly, so it's not really a limitation.



来源:https://stackoverflow.com/questions/48368690/recursive-sftp-listdir-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!