Python win32com CallNotImplementedError 而不是 AccessDenied?

Python win32com CallNotImplementedError instead of AccessDenied?

此代码:

import os
from win32com.shell import shell, shellcon

tempFile = os.path.join(os.path.abspath(os.path.curdir), u'_tempfile.tmp')
# print tempFile
dest = os.path.join('C:\Program Files', '_tempfile.tmp')
with open(tempFile, 'wb'): pass # create the file
try: # to move it into C:\Program Files
        result, aborted = shell.SHFileOperation(
                (None, # parent window
                 shellcon.FO_MOVE, tempFile, dest,
                 # 0,
                 shellcon.FOF_SILENT, # replace this with 0 to get a UAC prompt
                 None, None))
        print result, aborted
except: # no exception raised
    import traceback
    traceback.print_exc()

打印 120 False - 120 being CallNotImplementedError。如果标志设置为 0,那么您将按预期收到 UAC 提示。现在为什么不是结果 5 (AccessDeniedError) ?是不是确实没有实现或者是错误还是我没有得到什么?

不用说这很难调试 - 我原以为访问被拒绝,我不得不非常仔细地查看问题所在。

您误读了错误代码。 SHFileOperation uses its own set of error codes separate from the system error codes;在这种情况下,0x78/120 是:

DE_ACCESSDENIEDSRC: Security settings denied access to the source.

这似乎不是可能的错误(目标是问题所在),但此功能已被弃用(在 Vista 中被 IFileOperation 取代),虽然它在 Vista+ 中仍然存在,但它们可能不会在为 Vista 之前不存在的情况(如 UAC)返回完全准确的错误代码时特别小心。

根据文档:

  • These are pre-Win32 error codes and are no longer supported or defined in any public header file. To use them, you must either define them yourself or compare against the numerical value.
  • These error codes are subject to change and have historically done so.
  • These values are provided only as an aid in debugging. They should not be regarded as definitive.