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.