List all folders on a NAS with Python

lojoma picture lojoma · May 4, 2016 · Viewed 7.5k times · Source

I am trying to list all shared folders on a Synology NAS with Python.

So let's say I want a list of every folder on \\DISKSTATION
E.g.: public, homes, movies, etc

Is that possible?

Thanks for your help!

Answer

fips picture fips · May 4, 2016

Why not use pysmb?

pip install pysmb

To list share contents:

from smb.SMBConnection import SMBConnection

conn = SMBConnection('username', 'password', 'local_NetBIOS_name', 'remote_NetBIOS_name')
conn.connect('ip_address')
results = conn.listPath('share_name', '/optionally/some/subfolder')

for x in results:
    print x.filename

Will output:

.
..
dir1
dir2
file1
file2