0

I'm using Python 2.7 to monitor disk usage of some application running on Windows 2012 server. How can I get a size of some network storage folder located for example here:

\\storage\my_folder\

I've tried to use (from this post: calculating-a-directory-size-using-python ):

import os

def getFolderSize(folder):
    total_size = os.path.getsize(folder)
    for item in os.listdir(folder):
        itempath = os.path.join(folder, item)
        if os.path.isfile(itempath):
            total_size += os.path.getsize(itempath)
        elif os.path.isdir(itempath):
            total_size += getFolderSize(itempath)
    return total_size

but of course network path is not supported.

piet.t
  • 11,718
  • 21
  • 43
  • 52
Dima Kreisserman
  • 663
  • 1
  • 13
  • 33

2 Answers2

1

If it's a 2012 (Windows) server you may be able to use SMB.

Here's a small test program I just used to get the size of a shared folder on a Windows server. I haven't tested it exhaustively, so it may need some work, but it should give you a basis to work from.

It uses pysmb

from smb import SMBConnection

sep = '\\'

def RecursiveInspector(conn, shareName, path):
    #print path.encode('utf8')
    localSize = 0
    response = conn.listPath(shareName, path, timeout=30)
    for i in range(len(response)):
        fname = response[i].filename
        if (fname == ".") or (fname == ".."):
            continue
        if (response[i].isDirectory):
            dname = path
            if not (path.endswith(sep)):
                dname += sep
            dname += fname
            localSize += RecursiveInspector(conn, shareName, dname)
        else:
            localSize += response[i].file_size
    return localSize

conn = SMBConnection.SMBConnection("my_username",
                                   "my_password",
                                   "laptop",
                                   "the_shared_folder_name",
                                   use_ntlm_v2 = True)
conn.connect("1.2.3.4", 139)

path      = sep # start at root
totalSize = RecursiveInspector(conn, "the_shared_folder_name", path)

print totalSize

Hope this may be useful.

AS Mackay
  • 2,831
  • 9
  • 19
  • 25
  • could you please assist me to modify the script so it can be used on storage folder? Seems that the SMBConnection.SMBConnection function requires a "remote_name" of the machine where the shared folder located. How can I use this in case of a storage location? – Dima Kreisserman Dec 10 '17 at 11:43
0

Thanks to this post using win32com.client enabled me to get a size of a network folder!

Dima Kreisserman
  • 663
  • 1
  • 13
  • 33