3

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.

1 Answer 1

2

From experimenting on my machine, this appears to be associated with the permissions for two junction points:

C:\Users\Adam\AppData\Local\Microsoft\Windows\INetCache\Content.IE5

which points to

C:\Users\Adam\AppData\Local\Microsoft\Windows\INetCache\IE

and

C:\Users\Adam\AppData\Local\Microsoft\Windows\INetCache\Low\Content.IE5

which points to

C:\Users\Adam\AppData\Local\Microsoft\Windows\INetCache\Low\IE]

The strange thing is that both of those destinations are accessible so it is access rights for the junctions rather that their destinations.

This answer discusses the issues with this particular file.

This answer explains how the Size property is working and why it fails when it encounters folders without adequate permissions. It looks like Size is trying to traverse the junction point and failing to do so due to the junction permissions.

The resolution for this problem may depend to what degree you require the size of the user profile folder including AppData. If you aren't concerned about AppData then you could just sum the size of all the subdirs.

Alternatively, if you do require the size of AppData then you could get the size of all the folders (including AppData) and for those that error, presumably just AppData, you can traverse down the hierarchy summing the folder sizes where possible until you hit something that you can't navigate into (i.e. the junctions).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.