使用 python 列出网络共享

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1459590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:18:43  来源:igfitidea点击:

listing network shares with python

pythonnetworkingdrive

提问by Gearoid Murphy

if I explicitly attempt to list the contents of a shared directory on a remote host using python on a windows machine, the operation succeeds, for example, the following snippet works fine:

如果我在 Windows 机器上使用 python 明确尝试列出远程主机上共享目录的内容,则操作成功,例如,以下代码段工作正常:

os.listdir("\\remotehost\share")

However, if I attempt to list the network drives/directories available on the remote host, python fails, an example of which is shown in the following code snippet:

但是,如果我尝试列出远程主机上可用的网络驱动器/目录,python 将失败,以下代码片段中显示了一个示例:

os.listdir("\\remotehost")

Is anyone aware of why this doesn't work?, any help/workaround is appreciated.

有没有人知道为什么这不起作用?,感谢任何帮助/解决方法。

回答by Victor Kotseruba

May be pysmbcan help

可能是pysmb可以提供帮助

回答by jwlitts

For anyone still wondering how to list network shares at the top level on windows, you can use the win32net module:

对于仍然想知道如何在 Windows 的顶层列出网络共享的任何人,您可以使用 win32net 模块:

import win32net
shares, _, _ = win32net.NetShareEnum('remotehost',0)

The integer controls the type of information returned but if you just want a list of the shares then 0 will do.

整数控制返回的信息类型,但如果您只想要共享列表,则 0 即可。

This works where os.listdir('\\remotehost') fails as '\\remotehost' isn't a real folder although windows can display it like one.

这适用于 os.listdir('\\remotehost') 失败,因为 '\\remotehost' 不是一个真正的文件夹,尽管 Windows 可以像一个文件夹一样显示它。

回答by Troy

I'm sure the OP has forgotten about this question by now, but here's (maybe) an explanation:

我确定 OP 现在已经忘记了这个问题,但这里(也许)有一个解释:

http://www.python.org/doc/faq/windows/#why-does-os-path-isdir-fail-on-nt-shared-directories

http://www.python.org/doc/faq/windows/#why-does-os-path-isdir-fail-on-nt-shared-directories

In case anybody else happens along this problem, like I did.

万一其他人遇到这个问题,就像我一样。

回答by Santi

Sorry. I'm not able to try this as I'm not in a PC. Have you tried:

对不起。我无法尝试此操作,因为我不在 PC 上。你有没有尝试过:

os.listdir("\\remotehost\")