2

My test code is:

#!/usr/bin/env python
import win32com.client

def GetFolderSizeQuick(target_folder):
    fso = win32com.client.Dispatch("Scripting.FileSystemObject")
    fobj = fso.GetFolder(target_folder)
    return fobj.size

print(GetFolderSizeQuick("d:/pytools"))
print(GetFolderSizeQuick("d:/cygwin"))

The result is:

D:\>python a.py
160659697
Traceback (most recent call last):
  File "a.py", line 10, in <module>
    print(GetFolderSizeQuick("d:/cygwin"))
  File "a.py", line 7, in GetFolderSizeQuick
    return fobj.size
  File "D:\Applications\Python33\lib\site-packages\win32com\client\dynamic.py",
line 511, in __getattr__
    ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
pywintypes.com_error: (-2147352567, '发生意外。', (0, None, None, None, 0, -2146828218), None)

The first call GetFolderSizeQuick on d:/pytools folder works. it's about 153MB. But the second call failed. The folder d:/cygwin is about 12.6GB.

I am working on windows 7 with python3.3.0 32bit version. So I think the problem happened on the 32bit or 64bit to store the result. 32bit int can not store 12.6GB size.

What is the real problem here, and how to fix it?

truease.com
  • 1,261
  • 2
  • 17
  • 30

1 Answers1

3

That's neither a directory size nor a 32/64-Bit problem. It's even not a python2 or python3 problem.

Your Error translates to "No Access allowed!"

The simpliest way for testing would be to create a directory where only the owner is allowed to read and all others have NO rights at all. Then take this directory as input - you'll get the same error, even if the directory is empty. A good example would be the local "c:\system Volume Information".

Digging a little deeper: The errorcodes given by python are signed, whereas for a reasonable lookup Microsoft describes and expects them as unsigned. Kudos to EB in this thread and Tim Peters in this thread, using the examples, you'll get reasonable error-Codes.

import win32com.client
import pywintypes

def get_folder_size(target_folder):
    fso = win32com.client.Dispatch("Scripting.FileSystemObject")
    fobj = fso.GetFolder(target_folder)
    return fobj.size


if __name__ == '__main__':
    try:
        get_folder_size('c:/system volume information')
    except pywintypes.com_error, e:
        print e  # debug, have to see which indices
        print hex(e[0]+2**32), hex(e[2][5]+2**32)

Now search for both of the hex digits, the 2nd one should lead to a lot of "you are not allowed to..." queries and answers.

Community
  • 1
  • 1
antiphasis
  • 86
  • 4