I'm using a solution posted by the user Augustin here: very quickly getting total size of folder
folderPath = r"C:\Users\e46ldc\Documents"
fso = com.Dispatch("Scripting.FileSystemObject")
folder = fso.GetFolder(folderPath)
MB = 1024 * 1024.0
print("%.2f MB" % (folder.Size / MB))
The exception that occurs is as follows, and only happens on C:\Users\(user) at the top level, or on Documents. I can run it perfectly fine on folders further down (i.e. Downloads, or folders within Documents):
Traceback (most recent call last):
File "C:\Users\e46ldc\AppData\Local\Programs\Python\Python37-32\win32dirinfo.py", line 14, in <module>
print("%.2f MB" % (folder.Size / MB))
File "C:\Users\e46ldc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32com\client\dynamic.py", line 516, in __getattr__
ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2146828218), None)
After a lot of digging, it seems that this is a permissions error. But, even when running the script as an administrator, I still encounter it. I've verified that it's finding the folder correctly, and I can access the folder fine with other methods like os.walk() or scandir.
Is there any way around this? This method is the fastest I've tested to get the size of a directory on Windows so I'm hoping I can still use it.