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

查看:97
本文介绍了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

这个功能一直运行得很完美,直到今天它无故停止工作,我没有改变任何东西.最让我困惑的是该函数运行正常,如果我运行它后打印出'freespace'的值,它已经找到了正确的值,但仍然给出错误.

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.

可能导致此问题的原因是什么?

What could have caused this issue?

推荐答案

您没有调用完整的函数签名,我认为这会导致访问冲突(由于随机内存写入)和错误.完整的函数签名是(完整记录的这里):

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天全站免登陆