1
yakczh 2014-11-19 11:46:06 +08:00
python3 用 open(file,mod='r',encodineg='utf8') 打开一个非utf8编码的文件,会报异常,利用这一点,可以识别出utf8编码的文件
|
2
aazzsx OP @yakczh 好像不是一个问题……python3在shell打开一个不存在的文件时弹出 FileNotFoundError 而在 .py文件里面可以用IOError捉到错误,用FileNotFoundError出错没有定义?
|
3
staticor 2014-11-19 12:06:45 +08:00
应该是对异常类的重新细分吧, 在3.2-3.4版本更新中就有些变化 3.1不清楚是怎么分的.
可参考http://devdocs.io/python/library/exceptions#IOError |
4
gateswong 2014-11-19 12:18:42 +08:00
python 3.2之后把IOError 替换成了FileNotFoundError
|
5
gateswong 2014-11-19 12:23:20 +08:00
https://docs.python.org/3.2/library/functions.html#open
> Open file and return a corresponding file object. If the file cannot be opened, an IOError is raised. https://docs.python.org/3.3/library/functions.html#open > Open file and return a corresponding file object. If the file cannot be opened, an OSError is raised. https://docs.python.org/3.3/library/exceptions.html#FileNotFoundError > exception FileNotFoundError > Raised when a file or directory is requested but doesn’t exist. Corresponds to errno ENOENT. 3.2里没有这个异常 |