Python:WindowsError:异常:访问冲突读取为0x00000000 [英] Python: WindowsError: exception: access violation reading 0x00000000

查看:1061
本文介绍了Python:WindowsError:异常:访问冲突读取为0x00000000的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个短函数只是获取存储设备的可用空间,但是在运行代码时出现上述错误。

This short function is simply getting the available free space of a storage device, however I am getting the above error when running the code.

该函数是:

def disk_space1(drive):
    freespace = ctypes.c_ulonglong()
    calcspace = ctypes.windll.kernel32.GetDiskFreeSpaceExA
    calcspace(drive, ctypes.byref(freespace))
    disk_size = freespace.value
    return disk_size

此功能一直有效,直到今天无故停止运行,我没有做任何更改。最让我感到困惑的是该函数正常运行,如果我在运行它后打印出自由空间的值,它就消失了,找到了正确的值,但仍然给出了错误。

This function worked perfectly until today when it has stopped working for no reason, I haven't changed anything. What's baffling me the most is that the function works properly, if I print out the value of 'freespace' once running it, it has gone and found the correct value, but still gives the error.

是什么引起了此问题?

推荐答案

您没有调用我认为是的完整功能签名导致访问冲突(由于随机存储器写入)和错误。完整功能签名(完整记录为 ):

You are not calling the full function signature which I believe is leading to access violations (due to random memory writes) and errors. The full function signature is (fully documented here):

BOOL WINAPI GetDiskFreeSpaceEx(
  _In_opt_   LPCTSTR lpDirectoryName,
  _Out_opt_  PULARGE_INTEGER lpFreeBytesAvailable,
  _Out_opt_  PULARGE_INTEGER lpTotalNumberOfBytes,
  _Out_opt_  PULARGE_INTEGER lpTotalNumberOfFreeBytes
);

通过将函数更改为:

def disk_space(drive):
    freespace = ctypes.c_ulonglong()
    calcspace = ctypes.windll.kernel32.GetDiskFreeSpaceExA
    err = calcspace(drive,
                    ctypes.byref(freespace),
                    None,
                    None)
    assert err != 0, 'calcspace failed'
    disk_size = freespace.value
    return disk_size

我能够运行它而不会出现间歇性错误。

I was able to run it without intermittent error.

这篇关于Python:WindowsError:异常:访问冲突读取为0x00000000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆