List shared folders

Introduction

Which folders are shared on a computer? There are at least two -- and I suspect several more -- ways of getting to this information. As far as I'm aware, there's no particular reason to choose one over the other; it comes down to what you're comfortable with.

Option one is to use WMI; this is already illustrated by an example on the WMI cookbook on this very site. A second approach is to use the win32net module from pywin32.

import win32net
import win32netcon

COMPUTER_NAME = "" # look at this machine
INFO_LEVEL = 2

resume = 0
while 1:
  (shares, total, resume) = \
    win32net.NetShareEnum (
      COMPUTER_NAME,
      INFO_LEVEL,
      resume,
      win32netcon.MAX_PREFERRED_LENGTH
    )
  for share in shares:
    print share['netname'], "=>", share['path']
  if not resume:
    break