1

I need to make room on a drive that has many files. I want to find which folders are taking up the most space in a specific location called C:\RS_Data. Once I determine which of the sufolders are taking up the most space I want to drill down into those subfolders and perform the same task. So Iw ould like to develop a script that will work with me specifying the path to return the file and folder sizes, It could be something like this: C:\RS_Data: Sub_Folder_1: 566,000 kb Sub_Folder_2: 323,333 kb Sub_folder_3: 3,123,456 kb random_file.ext: 3,123 kb

and so on...

I would then like to run the same script on say Sub_folder_3.

I found the following script on the forum and added paths.append(path at line 22:

import locale
import os

locale.setlocale(locale.LC_ALL, "")

def get_size(state, root, names):
    paths = [os.path.realpath(os.path.join(root, n)) for n in names]
    # handles dangling symlinks
    state[0] += sum(os.stat(p).st_size for p in paths if os.path.exists(p))

def print_sizes(root):
    total = 0
    paths = []
    state = [0]
    n_ind = s_ind = 0
    for name in sorted(os.listdir(root)):
        path = os.path.join(root, name)
        if not os.path.isdir(path):
            continue
        paths.append(path)

        state[0] = 0
        os.path.walk(path, get_size, state)
        total += state[0]
        s_size = locale.format('%8.0f', state[0], 3)
        n_ind = max(n_ind, len(name), 5)
        s_ind = max(s_ind, len(s_size))
        paths.append((name, s_size))

    for name, size in paths:
        print name.ljust(n_ind), size.rjust(s_ind), 'bytes'
    s_total = locale.format('%8.0f', total, 3)
    print '\ntotal'.ljust(n_ind), s_total.rjust(s_ind), 'bytes'

print_sizes('.')

I got an error that there were too many values to unpack. Is there a way aI can run this with the script working on a path that I specify?

Thanks.

I took the sample recommended below and modified it so the function could be run in a for loop. This is returning a 0 for all files and folders in the parent folder. Here is the modified code:

import os

myDir = "C:\\RS_Data"
folders = os.listdir(myDir)

for file in folders: 
    def get_size(start_path = file):
        global total_size
        total_size = 0
        for dirpath, dirnames, filenames in os.walk(start_path):
            for f in filenames:
                fp = os.path.join(dirpath, f)
                total_size += os.path.getsize(fp)
        return total_size

    print os.path.basename(file),":", get_size()

Here is the result:

Attribute.gdb : 0
Bookmarks : 0
calculations : 0
Common : 0
Copy of CO_054_Run Batch Files.bat : 0
CO_003_Run Batch Files.bat : 0
CO_052_Run Batch Files.bat : 0
CO_053_Run Batch Files.bat : 0
CO_054_2_Run_Batch_Files.bat : 0
CO_054_Run Batch Files.bat : 0
CO_073_Run Batch Files_Old.bat : 0
CO_073_Run_Batch_Files.bat : 0
CO_073_Run_Batch_FilesREV1.bat : 0
gdb backups : 0
GeocodingInformation.gdb : 0
GeocodingInformation.ldb : 0
GeocodingInformation.mdb : 0
GeocodingInformationold.mdb : 0
GIS_Projects : 0
Models : 0
Network : 0
Non_RS_MXDs : 0
old2Run Batch Files.bat : 0
old3Run Batch Files.bat : 0
OldCO_052_053_054_Run Batch Files2.bat : 0
OldCO_052_053_054_Run Batch FilesIJ.bat : 0
oldCO_052_Run Batch Files.bat : 0
oldCO_053_Run Batch Files.bat : 0
oldCO_073_Run Batch Files.bat : 0
orig_rs_system.mdb : 0
orig_rs_system_backup.mdb : 0
PatternGroup.gdb : 0
Python : 0
Recovered : 0
robocopy.exe : 0
ROBOUSERS.OUT : 0
rs_system.mdb : 0
rs_system_backup.mdb : 0
rs_system_backupnew.ldb : 0
rs_system_backupnew.mdb : 0
Run Batch Files_old.bat : 0
Scrap : 0
Temp : 0
Templates.gdb : 0
Workspace : 0
WorkspacesSettings.gdb : 0

If I run the script without a loop it returns the accumulative size of the specified folder.

geoJshaun
  • 637
  • 2
  • 11
  • 32

1 Answers1

0

There is a similar question at: Calculating a directory size using Python?

import os
    def get_size(start_path = '.'):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            total_size += os.path.getsize(fp)
    return total_size
print get_size()
Community
  • 1
  • 1
James Vickery
  • 732
  • 3
  • 10
  • 23
  • Yes. I saw this as well but keep getting "UnboundLocalError: local variable 'toatl_size' referenced before assignment". Also I don't see where I can specify the folder I want the results returned on. – geoJshaun Jun 02 '16 at 18:00
  • @Shaun UnboundLocalError is to do with trying to use a variable before it's been initialised. Where have you first defined 'total_size'? Try putting 'global total_size' at the start of the function that modifies its value. Also, did you copy that error message directly? If so, the problem is that you've spelt 'total_size' wrong (you said 'toatl_size') – James Vickery Jun 02 '16 at 18:13
  • 1
    That was it. toatl, not total. But still this is returning one size for what I believe the root directory is. I'm going to try and loop through it to print folders and and file names with associated sizes. – geoJshaun Jun 02 '16 at 18:35
  • @Shaun Most of my mistakes are typos too! Have you seen [http://www.sfentona.net/?p=2641](http://www.sfentona.net/?p=2641)? Something like this would work if you were to simply calculate the sum of the sizes of each subdirectory – James Vickery Jun 02 '16 at 18:45
  • Thanks James. That sfentona script works well enough for me. I can't import humanize or pwd because I don't have admin rights but excluding them from the code still returns what I need for a comparison basis. – geoJshaun Jun 02 '16 at 19:05
  • @Shaun Glad I could help – James Vickery Jun 02 '16 at 19:13