- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
如何在 python 3 上发现错误?我用谷歌搜索了很多,但似乎没有一个答案有效。文件 open.txt 不存在,所以它应该打印 e.errno。
这是我现在尝试的:
这是我定义的函数
try:
with open(file, 'r') as file:
file = file.read()
return file.encode('UTF-8')
except OSError as e:
print(e.errno)
但是当我得到这个错误时我没有打印任何东西
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
最佳答案
FileNotFoundError
是 OSError
的子类,捕获它或异常本身:
except OSError as e:
操作系统异常已在 Python 3.3 中重新设计; IOError
已合并到 OSError
。见 PEP 3151: Reworking the OS and IO exception hierarchy section在 What's New 文档中。
更多详情,OS Exceptions section如需更多信息,请向下滚动查看类层次结构。
也就是说,您的代码应该仍然可以工作,因为 IOError
现在是 OSError
的别名:
>>> IOError
<class 'OSError'>
确保您将异常处理程序放置在正确的位置。仔细查看异常的回溯,以确保您没有错过实际引发的位置。最后但同样重要的是,您确实重新启动了 Python 脚本,对吧?
关于python - FileNotFoundError "try .. except IOError"未捕获时如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28633555/
from os import path from pydub import AudioSegment input_file = "audio.mp3" output_file = "result.wa
尝试运行 mask_rcnn 的示例程序颜色飞溅并收到此错误: 我正在环境中的 anaconda 上运行 mrcnn 模型masknet,并安装了以下软件包。我已按照 https://github
我正在尝试使用 PySerial 来接受来自 RFID 阅读器的输入。根据 here: 的答案,我尝试使用 WinObj 并发现一些奇怪的东西:COM3 文件夹中没有 GLOBAL??? 端口指向“更
我有一个奇怪的问题。我既不能重命名特定文件,也不能删除它们。我收到 FileNotFoundError。 以前也有人问过类似的问题。此问题的解决方案是使用完整路径,而不仅仅是文件名。 我的脚本在仅使用
我有一个文本文件“Flickr_8k.testImages.txt”,其中包含由换行符分隔的 1000 个文件的文件名。这些文件位于目录“Flickr8k_Dataset”内,其中包含 8000 多个
我正在尝试制作一个简单的程序,将具有用户指定扩展名的所有文件复制到另一个文件夹中。代码如下,错误如下: #! python3 # Copies all files from folder with u
我的 Python 应用程序包含一个名为 Tests 的子文件夹,我用它来运行单元测试。我的所有文件都位于父文件夹中,我将其称为 App。例如,Tests 文件夹包含一个 test.py 文件。 Ap
我有一个函数,它获取包含时间的源文件(csv 文件),读取它,然后按顺序对行进行排序并将它们写入目标文件中。但是,如果源 csv 文件不存在,我需要引发 FileNotFoundError。我之前曾引
我收到 FileNotFoundError当用户尝试注册我的网站并上传照片时出现错误消息。错误消息如下: [Errno 2] No such file or directory: '/tmp/.upl
我正在尝试关注 this blog关于如何从 Python 执行 R 脚本。我使用 Rscript 从命令行运行 R 脚本。 这是我的 Python 代码: import subprocess imp
使用 Windows 7、Python 3.5.1: import subprocess subprocess.check_output(['echo', 'hello']) 引发错误: Traceb
这个问题在这里已经有了答案: open() gives FileNotFoundError/IOError: Errno 2 No such file or directory (8 个答案) 关闭
我使用的第三方库很好,但不会按照我想要的方式处理不存在的文件。当给它一个不存在的文件时,而不是提高旧的 FileNotFoundError: [Errno 2] No such file or dir
我已经使用 matplotlib 制作了一个图形,现在我正在尝试使用 matplotlib.pyplot.savefig 将其保存到文件中 import matplotlib.pyplot as pl
您好,我正在关注 pygame 游戏代码,但出现以下错误并发布了一个问题。当我运行它时,屏幕弹出然后立即消失。背景颜色也没有保存就执行了。 (Mac 操作系统) import pygame pygam
刚刚回到Python,我正在尝试构建一个脚本来匹配文件名,重命名,压缩它们,然后最终构建一个控制文件(我还没有写过)。它适用于放置在目录中的文件,但最后我收到错误: FileNotFoundError
我在使用 Python 的 Mac OS X 中遇到了相当奇怪的竞争条件(我只测试过 Python 3.3)。我正在创建几个临时目录,向其中写入内容,然后清除它们。类似于 while running:
为什么下面的交互失败? (Python 3.6.1) >>> with open('an_image.png', 'rb') as f, open('~/Desktop/an_image.png',
img = printscreen_pil img = img.filter(ImageFilter.MedianFilter()) enhancer = ImageEnhance.Contrast(
使用 unittest 模块,如何测试打开文件时引发的 FileNotFoundError,如下所示: func(filename): try: file = open(fil
我是一名优秀的程序员,十分优秀!