I created a class named Options. It works fine but not not with Python 2. And I want it to work on both Python 2 and 3. The problem is identified: FileNotFoundError doesn t exist in Python 2. But if I use IOError it doesn t work in Python 3

Changed in version 3.3: EnvironmentError, IOError, WindowsError, VMSError, socket.error, select.error and mmap.error have been merged into OSError.


You can use the base class exception EnvironmentError and use the 'errno' attribute to figure out which exception was raised:

from __future__ import print_function

import os
import errno try:
open('no file of this name') # generate 'file not found error'
except EnvironmentError as e: # OSError or IOError...
print(os.strerror(e.errno))

Or just use IOError in the same way:

try:
open('/Users/test/Documents/test') # will be a permission error
except IOError as e:
print(os.strerror(e.errno))

That works on Python 2 or Python 3.

Be careful not to compare against number values directly, because they can be different on different platforms. Instead, use the named constants in Python's standard library errno modulewhich will use the correct values for the run-time platform.

Searching for equivalent of FileNotFoundError in Python 2的更多相关文章

  1. 【转】linux和windows下安装python集成开发环境及其python包

    本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...

  2. lib/sqlalchemy/cextension/processors.c:10:20: 致命错误: Python.h:没有那个文件或目录

    本文地址:http://www.cnblogs.com/yhLinux/p/4063444.html $ sudo easy_install sqlalchemy [sudo] password fo ...

  3. Python easy_install

    系统中有高版本的Python, 直接pip3 install ipcalc安装,都是装到高版本的Python 系统默认的Python是2.7.6,现在想装到默认版本中,可以使用easy_install ...

  4. selenium python 环境搭建(64位 windows)

    之前写了同样的文章,可是后来自己按照给文章再次搭建环境当搭建环境成功后却发现还是无法用.使用from selenium import webdriver,在run的时候却出现ImportError: ...

  5. 使用国内镜像通过pip安装python的一些包 Cannot fetch index base URL http://pypi.python.org/simple/

    原文地址:http://www.xuebuyuan.com/1157602.html 学习flask,安装virtualenv环境,这些带都ok,但是一安装包总是出错无法安装, 比如这样超时的问题: ...

  6. Python Scrapy安装杂症记录

    昨天安装了scrapy一切正常,调试了bbsSpider案例(详见上文),今日开机因为冰封还原,提示找不到python27.dll,重新安装了python2.7, 使用easy-install scr ...

  7. Python 使用pymongo操作mongodb库

    Python 使用pymongo操作mongodb库 2016-12-31 21:55 1115人阅读 评论(0) 收藏 举报  分类: - - - Python(10)  版权声明:本文为博主原创文 ...

  8. Python网络编程 - 请求地址上的文件并下载

    我们用到了requests库,由于是第三方的,必须下载 如果是python 2.x用下面命令 pip install requests python 3.x用下面命令 easy_install req ...

  9. python easy_install 发生Unable to find vcvarsall.bat错误的处理方法

    用python安装mmseg分词包时发生了 Unable to find vcvarsall.bat 错误 Searching for mmseg Reading http://pypi.python ...

随机推荐

  1. 关于面试总结2-SQL学生表

    前言 接着上一篇https://www.cnblogs.com/yoyoketang/p/10065424.html,继续学生表SQL 1.计算每个人的平均成绩, 要求显示字段: 学号,姓名,平均成绩 ...

  2. [Linux] ubuntu各目录含义

    /boot/: 启动文件,所有与系统启动有关的文件都保存在这里 /boot/grub/:grub引导器相关的配置文件都在这里 /dev/:此目录中保存了所有设备文件,例如,使用的分区:/dev/hda ...

  3. iOS:仿写探探App动画

    一.简单介绍 探探动画比较新颖,这也是它在众多交友软件中火热的一个特色.实现这种动画的方式可以有两种方式实现: 1.使用转场动画实现  2.使用CollectionView自定义布局实现, 此处我提供 ...

  4. Python traceback 模块, 打印异常信息

    Python感觉是模仿Java, 到处都需要加try..catch.... 这里记录一下用法,方便后续使用. # -*- coding:utf-8 -*- import os import loggi ...

  5. 算法:快速排序实现 & 定制比较函数

    1. 快速排序基本算法 #include<stdio.h> ; int quick_sort(int *a, int start, int end){ if (start >= en ...

  6. 解决easyui combobox不能默认选中

    开始出现很奇怪的问题,ff没有问题IE8还是会出现不能选中默认项的问题,更改了select.input的方式,数据加载方式也更改为json的方式,未果,最后将datagrid toolbar的初始化放 ...

  7. [leetcode]Pascal's Triangle II @ Python

    原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/ 题意: Given an index k, return the kth row ...

  8. Path Sum II leetcode java

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  9. Vue背景图打包之后访问路径错误

    问题背景:项目里面有用到背景图片,开发模式下正常,打包之后发现报404错误.查看发现是背景图片引用路径出错. 解决方法: .map { width: %; height: 397px; backgro ...

  10. listview加载数据

    首先我们需要理清思路:使用ListView显示数据是很方便的,ListVIew的数据之间通过适配器adapter去作为桥梁连接起来.当我们需要使用listview显示大量数据的时候,我们需要使用到分页 ...