Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
应为LP_SHFILEOPSTRUCTW实例,而不是指向SHFILEOPSTRUCTW(python ctypes)的指针_Python_Winapi_Ctypes_Win32com - Fatal编程技术网

应为LP_SHFILEOPSTRUCTW实例,而不是指向SHFILEOPSTRUCTW(python ctypes)的指针

应为LP_SHFILEOPSTRUCTW实例,而不是指向SHFILEOPSTRUCTW(python ctypes)的指针,python,winapi,ctypes,win32com,Python,Winapi,Ctypes,Win32com,我使用以下代码在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.abs

我使用以下代码在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_noerrorrui | shellcon.FOF_noconfirmkdir

op代表例如:
shellcon.FO_COPY

我的问题是,有时此函数会给我错误:

ArgumentError: argument 1: <type 'exceptions.TypeError'>: expected LP_SHFILEOPSTRUCTW instance instead of pointer to SHFILEOPSTRUCTW 
ArgumentError:参数1::应为LP_SHFILEOPSTRUCTW实例,而不是指向SHFILEOPSTRUCTW的指针
特别是在处理非常长的路径时(例如
len(dest)=230

我做错了什么

[编辑]

有一个
shell.SHFileOperation
,但我们需要使用自定义包装器
SHFileOperationW
来支持Unicode

[编辑二]

正如Barmak Shemirani所写,在python3中,您只需使用shell.SHFileOperation,它就可以处理任何特殊的unicode字符。 如果我能在python2中找到解决方案,我将在这里分享。

在版本3中,您可以使用它,它还将在需要时附加双空终止符

from win32com.shell import shell, shellcon

shell.SHFileOperation((
    None,
    shellcon.FO_COPY,
    "c:\\test\\test1.txt\0c:\\test\\test2.txt",
    "c:\\test\\ελληνική+漢語+English",
    shellcon.FOF_SILENT | shellcon.FOF_NOCONFIRMATION | 
    shellcon.FOF_NOERRORUI | shellcon.FOF_NOCONFIRMMKDIR,
    None,
    None))

上述代码不起作用(导入、变量初始化丢失)。另外,
SHFileOperationW
返回一个int,索引它(
[0]
)将触发一个错误。我没有
win32com.shell
@CristiFati安装pywin32选项1-使用whl安装下载whl(如果页面加载出现问题,请转到并查找指向控制盘的链接)。下载与python安装程序版本兼容的whl(如果python是v32b,那么就下载v32b,即使windows本身是v64b)。使用pip安装路径安装\to\whl选项2-使用msi安装程序安装转到下载最新的下载exe安装程序安装它。对[0]的调用是错误的,我删除了它。在本例中,src和dst是您想要的任何路径,在我的代码中,这是一个func,并作为参数传递。您不使用
shutil
复制文件的原因是什么?另外,您是否超过了Windows
MAX_path
?它不支持Unicode,例如中文、泰米尔、,我用Unicode对它进行了测试,结果很好。我测试了你的
SHFileOperationW
方法,它也能正常工作。这显然与版本有关。您可以将
win32api.MessageBox
与Unicode一起使用吗?您可以尝试使用:Unicode('tmpvziliy')吗马]сâↄṈஅаƏŁĞƘ,“utf-8”),并确保它确实复制了该文件?shell.SHFileOperation返回(0,False)?我使用的是Python2.x,你使用的是Python3.x吗?是的,这是版本3.6,它使用Unicode。您可以在
SHFileOperation