2

How can I list the directories on a network other than using os.listdir()

This prompts me error

import os

path = r"\\123.12.12.123"
print(os.listdir(path))

FileNotFoundError: [WinError 67] The network name cannot be found: '\\\\123.12.12.123'

This is fine but NOT what I want

import os

path = r"\\123.12.12.123\abc"
print(os.listdir(path))

Tried all these and got the same error code.

path = "\\\\123.12.12.123"
path = "\\\\123.12.12.123\\"
path = "//123.12.12.123"

UPDATE:

it's a server address but not a valid UNC path. \123.12.12.123\abc is the actual root of a shared drive.

os.listdir() will not do the job, we will need to use some other alternative to list the share drives as mentioned in answer section.

Cheang Wai Bin
  • 133
  • 1
  • 7
  • 1
    @CheangWaiBin there is no such path as `\\123.12.12.123`, it's a server address but not a valid UNC path. `\\123.12.12.123\abc` is the actual root of a shared drive. – Masklinn Aug 12 '20 at 05:59
  • I think its duplicate. Please see this question in SO [list network win directory](https://stackoverflow.com/questions/14354113/retrieving-contents-from-a-directory-on-a-network-drive-windows/14354523]) – Adam Aug 12 '20 at 06:01
  • 1
    Does this answer your question? [Retrieving contents from a directory on a network drive (windows)](https://stackoverflow.com/questions/14354113/retrieving-contents-from-a-directory-on-a-network-drive-windows) – Adam Aug 12 '20 at 06:02
  • @Masklinn So it's impossible to get the directories of that server address am i right? – Cheang Wai Bin Aug 12 '20 at 06:07
  • @Adam No, the answer over there is getting the directories of the server address + 1 level, I tried the solution it doesnt work because i'm trying to get the directories of the server address, it seems like ```os.listdir``` is not able to get what I want, that's why i'm trying to look for other alternative. – Cheang Wai Bin Aug 12 '20 at 06:09
  • 1
    @CheangWaiBin not via `os.listdir`, [you need to use `net view` for this information](https://serverfault.com/questions/17710/how-can-i-get-a-list-of-shared-directories-on-local-windows-server). It is also available in the win32 API through [NetShareEnum](https://learn.microsoft.com/en-gb/windows/win32/api/lmshare/nf-lmshare-netshareenum?redirectedfrom=MSDN), which [is mapped in PyWin32](http://timgolden.me.uk/pywin32-docs/win32net__NetShareEnum_meth.html), so that's also an option though a more constraining one (as you need to install pywin32). – Masklinn Aug 12 '20 at 06:23
  • @Masklinn, the `pywin32` solution sounds interesting too! Could you post this as an answer, possibly with a short example or a good reference to it? (The Python library itself does not seem to be that well documented...) – wovano Aug 17 '20 at 06:46
  • @Masklinn, the API reference you linked seems to be helpful after all. It was rather trivial to get it working. I can post a working example, but don't want to "steal your credits" ;-) But if you don't want to post it as answer, I will. – wovano Aug 17 '20 at 06:51
  • 1
    @wovano feel free to post a real answer, I don't really dev *on* windows, I'm just dimly aware of pywin32 (and path UNC issues as they come up in cross-platform code), so I'm not going to write a "proper" answer. – Masklinn Aug 17 '20 at 08:32

3 Answers3

2

net view will get the directory of the server address

net view by default returns a table-like output, using -match and -replace to get rid of unneeded info

the output will be in byte form, decode('utf-8') will convert the byte to string

import subprocess

path = r"\\123.12.12.123"
directory = subprocess.Popen(['powershell', r"(net view {}) -match '\sDisk\s' -replace '\s+Disk.*'".format(path)], stdout=subprocess.PIPE)
result = [item.decode('utf-8') for item in directory.communicate()[0].splitlines()]
Cheang Wai Bin
  • 133
  • 1
  • 7
1

On Windows you could use the Win32 API function NetShareEnum.

The Python module pywin32 provides access to much of the Win32 API, including the NetShareEnum function, which is availabe as win32net.NetShareEnum. The easiest way to find your shares would be using the Python command [x[0] for x in win32net.NetShareEnum('123.12.12.123')].

To demonstrate how this could be used, the following example prints all directories for all shares found on localhost:

import os
import win32net

def get_shares(server):
    return [x[0] for x in win32net.NetShareEnum(server)]

def print_shares_and_directories(server):
    shares = get_shares(server)
    for share in shares:
        print('SHARE: %s' % share)
        path = '\\\\%s\\%s' % (server, share)
        try:
            files = os.listdir(path)
        except OSError:
            print('    (Directory listing failed)')
        else:
            for file in files:
                if os.path.isdir(os.path.join(path, file)):
                    print('    %s' % file)

print_shares_and_directories('localhost')

Credits to Masklinn, who provided the relevant information in a comment.

wovano
  • 4,543
  • 5
  • 22
  • 49
-3

How about this one?

path = "\\123.12.12.123"
print(path)
# '\\123.12.12.123'

Please try the code above.

Daisuke Akagawa
  • 484
  • 2
  • 9