Created
April 10, 2017 22:08
-
-
Save DavidPu/bb154891e2ac6205ee45d9e565ec6dc3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import sys | |
class FSO(object): | |
import win32com.client as com | |
import pywintypes | |
fso = com.Dispatch("Scripting.FileSystemObject") | |
def get_folder_size(self, folderpath): | |
try: | |
folder = self.fso.GetFolder(folderpath) | |
return folder.Size | |
except Exception as details: | |
# com error pywintypes.com_error which is can't be specified extractly here... | |
if str(sys.exc_info()[0]).find('pywintypes.com_error') >= 0: | |
return None | |
else: | |
raise | |
Fso = FSO() | |
def traverse_dirs(tdir, maxdepth=4): | |
unhdl_dirs = [] | |
for dirpath, dirnames, filenames in os.walk(tdir): | |
d = dirpath[len(tdir):] | |
depth = len(d.split(os.path.sep)) | |
if depth > maxdepth: | |
dirnames.clear() | |
continue | |
if dirpath == topdir or not os.path.isdir(dirpath): | |
continue | |
size = Fso.get_folder_size(dirpath) | |
yield (size, dirpath) | |
topdir = r"C:\\" | |
unhandled_dirs = [] | |
for size, path in traverse_dirs(topdir, maxdepth=4): | |
if size == None: | |
if path not in unhandled_dirs and os.path.dirname(path) not in unhandled_dirs: | |
unhandled_dirs.append(path) | |
elif float(size) > 100 * 1024 * 1024.0: | |
print(path, '%.2f MB' % (size / 1024.0 / 1024.0)) | |
print(unhandled_dirs) | |
errdirs = [] | |
for tdir in list(set(unhandled_dirs)): | |
for size, path in traverse_dirs(tdir, maxdepth=3): | |
if size == None: | |
errdirs.append(path) | |
elif size and size > 100 * 1024 * 1024.0: | |
print(path, '%.2f MB' % (size / 1024.0 / 1024.0)) | |
print("\n".join(list(set(errdirs)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment