我使用以下代码在windows上执行一些文件系统操作(复制/移动/重命名文件和文件夹)。 此代码需要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_) + '\0', 'utf-8')
dest = unicode(os.path.abspath(_dest_) + '\0', '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
我的问题是,有时这个函数会给我错误:
^{pr2}$尤其是在处理很长的路径时(例如len(dest)=230
)
我做错什么了?在
[编辑]
有一个shell.SHFileOperation
,但我们需要使用自定义包装器SHFileOperationW
来支持Unicode。在
[编辑2]
正如Barmak Shemirani所写,在python3中,您只需使用shell.SHFileOperation它可以使用任何特殊的unicode字符。 如果我能在python2中找到解决方法,我会在这里分享。在
在版本3中,您可以将^{} 与^{} 一起使用,这也将在需要时附加双null终止符。在
相关问题 更多 >
编程相关推荐