listing network shares with python

荒凉一梦 提交于 2019-11-28 08:12:04

问题


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:

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:

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

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


回答1:


May be pysmb can help




回答2:


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

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.

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




回答3:


Maybe the following script will help you. See http://gallery.technet.microsoft.com/ScriptCenter/en-us/7338e3bd-1f88-4da9-a585-17877fa37e3b




回答4:


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

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.




回答5:


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

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


来源:https://stackoverflow.com/questions/1459590/listing-network-shares-with-python

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