预期 LP_SHFILEOPSTRUCTW 实例而不是指向 SHFILEOPSTRUCTW 的指针(python ctypes)
expected LP_SHFILEOPSTRUCTW instance instead of pointer to SHFILEOPSTRUCTW (python ctypes)
我正在使用以下代码在 windows 上执行一些文件系统操作(copy/move/rename 文件和文件夹)。
此代码需要 pywin32.
from win32com.shell import shell, shellcon
from ctypes.wintypes import HWND, UINT, LPCWSTR, BOOL, WORD
from ctypes import c_void_p, Structure, windll, POINTER, byref
src = unicode(os.path.abspath(_src_) + '[=12=]', 'utf-8')
dest = unicode(os.path.abspath(_dest_) + '[=12=]', 'utf-8')
class SHFILEOPSTRUCTW(Structure):
_fields_ = [("hwnd", HWND),
("wFunc", UINT),
("pFrom", LPCWSTR),
("pTo", LPCWSTR),
("fFlags", WORD),
("fAnyOperationsAborted", BOOL),
("hNameMappings", c_void_p),
("lpszProgressTitle", LPCWSTR)]
SHFileOperationW = windll.shell32.SHFileOperationW
SHFileOperationW.argtypes = [POINTER(SHFILEOPSTRUCTW)]
args = SHFILEOPSTRUCTW(wFunc=UINT(op), pFrom=LPCWSTR(src), pTo=LPCWSTR(dest), fFlags=WORD(flags), fAnyOperationsAborted=BOOL())
result = SHFileOperationW(byref(args))
aborted = bool(args.fAnyOperationsAborted)
if not aborted and result != 0:
# Note: raising a WindowsError with correct error code is quite
# difficult due to SHFileOperation historical idiosyncrasies.
# Therefore we simply pass a message.
raise WindowsError('SHFileOperationW failed: 0x%08x' % result)
标志总是:shellcon.FOF_SILENT | shellcon.FOF_NOCONFIRMATION | shellcon.FOF_NOERRORUI | shellcon.FOF_NOCONFIRMMKDIR
op 用于例如:shellcon.FO_COPY
我遇到的问题是有时这个函数会给我错误:
ArgumentError: argument 1: <type 'exceptions.TypeError'>: expected LP_SHFILEOPSTRUCTW instance instead of pointer to SHFILEOPSTRUCTW
尤其是在处理很长的路径时(例如len(dest)=230
)
我做错了什么?
[编辑]
有 shell.SHFileOperation
但我们需要使用自定义包装器 SHFileOperationW
来支持 unicode。
[edit2]
正如 Barmak Shemirani 所写,在 python3 中你可以简单地使用 shell.SHFileOperation 并且它可以处理任何特殊的 unicode 字符。
如果我能在 python2 中找到解决此问题的解决方案,我会在这里分享它。
在版本 3 中,您可以使用 shell.SHFileOperation
with SHFILEOPSTRUCT
,它还会在需要时附加双空终止符。
from win32com.shell import shell, shellcon
shell.SHFileOperation((
None,
shellcon.FO_COPY,
"c:\test\test1.txt[=10=]c:\test\test2.txt",
"c:\test\ελληνική+漢語+English",
shellcon.FOF_SILENT | shellcon.FOF_NOCONFIRMATION |
shellcon.FOF_NOERRORUI | shellcon.FOF_NOCONFIRMMKDIR,
None,
None))
我正在使用以下代码在 windows 上执行一些文件系统操作(copy/move/rename 文件和文件夹)。 此代码需要 pywin32.
from win32com.shell import shell, shellcon
from ctypes.wintypes import HWND, UINT, LPCWSTR, BOOL, WORD
from ctypes import c_void_p, Structure, windll, POINTER, byref
src = unicode(os.path.abspath(_src_) + '[=12=]', 'utf-8')
dest = unicode(os.path.abspath(_dest_) + '[=12=]', 'utf-8')
class SHFILEOPSTRUCTW(Structure):
_fields_ = [("hwnd", HWND),
("wFunc", UINT),
("pFrom", LPCWSTR),
("pTo", LPCWSTR),
("fFlags", WORD),
("fAnyOperationsAborted", BOOL),
("hNameMappings", c_void_p),
("lpszProgressTitle", LPCWSTR)]
SHFileOperationW = windll.shell32.SHFileOperationW
SHFileOperationW.argtypes = [POINTER(SHFILEOPSTRUCTW)]
args = SHFILEOPSTRUCTW(wFunc=UINT(op), pFrom=LPCWSTR(src), pTo=LPCWSTR(dest), fFlags=WORD(flags), fAnyOperationsAborted=BOOL())
result = SHFileOperationW(byref(args))
aborted = bool(args.fAnyOperationsAborted)
if not aborted and result != 0:
# Note: raising a WindowsError with correct error code is quite
# difficult due to SHFileOperation historical idiosyncrasies.
# Therefore we simply pass a message.
raise WindowsError('SHFileOperationW failed: 0x%08x' % result)
标志总是:shellcon.FOF_SILENT | shellcon.FOF_NOCONFIRMATION | shellcon.FOF_NOERRORUI | shellcon.FOF_NOCONFIRMMKDIR
op 用于例如:shellcon.FO_COPY
我遇到的问题是有时这个函数会给我错误:
ArgumentError: argument 1: <type 'exceptions.TypeError'>: expected LP_SHFILEOPSTRUCTW instance instead of pointer to SHFILEOPSTRUCTW
尤其是在处理很长的路径时(例如len(dest)=230
)
我做错了什么?
[编辑]
有 shell.SHFileOperation
但我们需要使用自定义包装器 SHFileOperationW
来支持 unicode。
[edit2]
正如 Barmak Shemirani 所写,在 python3 中你可以简单地使用 shell.SHFileOperation 并且它可以处理任何特殊的 unicode 字符。 如果我能在 python2 中找到解决此问题的解决方案,我会在这里分享它。
在版本 3 中,您可以使用 shell.SHFileOperation
with SHFILEOPSTRUCT
,它还会在需要时附加双空终止符。
from win32com.shell import shell, shellcon
shell.SHFileOperation((
None,
shellcon.FO_COPY,
"c:\test\test1.txt[=10=]c:\test\test2.txt",
"c:\test\ελληνική+漢語+English",
shellcon.FOF_SILENT | shellcon.FOF_NOCONFIRMATION |
shellcon.FOF_NOERRORUI | shellcon.FOF_NOCONFIRMMKDIR,
None,
None))