成功解决BUG:exception: access violation reading
异常解读
在 Python 代码编写过程中,会出现如下错误:
exception: access violation reading
该错误翻译为中文是:
文件访问冲突
实际编码错误如下图所示。
解决思路
检查传入给 OpenFile()
的参数是否有效即可,如果无效就会出现该BUG。
一般情况是文件路径不正确,调整路径即可完美解决问题。
错误复现
可以在 Python 文件中输入如下代码,即可出现本文标题所示错误:
import ctypes
invalid_argument = 12345 # 无效的参数
try:
ctypes.windll.kernel32.OpenFile(invalid_argument) # 调用一个接受无效参数的函数
except ValueError as e:
print(f"ValueError: {
e}")
except OSError as e:
print(f"OSError: {
e}")
except Exception as e:
print(f"An unexpected error occurred: {
e}")
其他学习资料
- 《滚雪球学Python》专栏与实体书:https://dream.blog.csdn.net/article/details/131268344
- 《爬虫100例》:https://blog.csdn.net/hihell/category_9280209.html
- 《Python爬虫120》:https://blog.csdn.net/hihell/category_11079529.html