python2.7以下出 NameError: global name 'FileNotFoundError' is not defined的解决方案
2017-08-28 16:51
579 查看
原文链接:http://blog.csdn.net/waiwai3/article/details/77461276
处理文件不存在使用FileNotFoundError来处理异常
Python版本:2.6
python代码:
[python]
view plain
copy
def count_words(filename):
try:
with open(filename) as f_obj:
contents = f_obj.read()
except FileNotFoundError:
msg = "Sorry, the file " + filename + " does not exist."
print(msg)
else:
words = contents.split()
num_words = len(words)
print("The file " + filename + " has about " + str(num_words) +" words.")
filename = 'alice.txt'
count_words(filename
运行结果:
[python]
view plain
copy
Traceback (most recent call last):
File "./count_words.py", line 15, in <module>
count_words(filename)
File "./count_words.py", line 6, in count_words
except FileNotFoundError:
NameError: global name 'FileNotFoundError' is not define
报错原因:
FileNotFoundError为python3使用的文本不存在异常处理方法
在python2.7中使用IOError
修改后的python代码
[python]
view plain
copy
#!/usr/bin/env python
def count_words(filename):
try:
with open(filename) as f_obj:
contents = f_obj.read()
#except FileNotFoundError:
except IOError:
msg = "Sorry, the file " + filename + " does not exist."
print(msg)
else:
words = contents.split()
num_words = len(words)
print("The file " + filename + " has about " + str(num_words) +" words.")
filename = 'alice.txt'
count_words(filename)
运行结果:
[python]
view plain
copy
Sorry, the file alice.txt does not exist
处理文件不存在使用FileNotFoundError来处理异常
Python版本:2.6
python代码:
[python]
view plain
copy
def count_words(filename):
try:
with open(filename) as f_obj:
contents = f_obj.read()
except FileNotFoundError:
msg = "Sorry, the file " + filename + " does not exist."
print(msg)
else:
words = contents.split()
num_words = len(words)
print("The file " + filename + " has about " + str(num_words) +" words.")
filename = 'alice.txt'
count_words(filename
运行结果:
[python]
view plain
copy
Traceback (most recent call last):
File "./count_words.py", line 15, in <module>
count_words(filename)
File "./count_words.py", line 6, in count_words
except FileNotFoundError:
NameError: global name 'FileNotFoundError' is not define
报错原因:
FileNotFoundError为python3使用的文本不存在异常处理方法
在python2.7中使用IOError
修改后的python代码
[python]
view plain
copy
#!/usr/bin/env python
def count_words(filename):
try:
with open(filename) as f_obj:
contents = f_obj.read()
#except FileNotFoundError:
except IOError:
msg = "Sorry, the file " + filename + " does not exist."
print(msg)
else:
words = contents.split()
num_words = len(words)
print("The file " + filename + " has about " + str(num_words) +" words.")
filename = 'alice.txt'
count_words(filename)
运行结果:
[python]
view plain
copy
Sorry, the file alice.txt does not exist
相关文章推荐
- NameError: name 'FileNotFoundError' is not defined的解决方案
- 关于python-NameError: global name 'platform' is not defined
- Python使用eval强制转换字符串为字典时报错:File "<string>", line 1, in <module> NameError: name 'nan' is not defined
- global name 'FileNotFoundError' is not defined
- GAE报错“NameError: global name 'execfile' is not defined”
- Python使用Tkinter错误,NameError: global name 'TOP' is not defined
- python实例2—NameError: name 'file' is not defined
- GAE报错“NameError: global name 'execfile' is not defined”
- Python编译时提示“NameError: name 'Popen' is not defined”及”NameError: name 'PIPE' is not defined“错误的解决
- Python 解决 :NameError: name 'reload' is not defined 问题
- 【安装】NameError: global name 'basestring' is not defined
- python 遇到NameError: name '__file__' is not defi...
- 【Python】NameError:name ‘xrange’ is not defined
- Python 解决 :NameError: name 'reload' is not defined 问题
- python 学习day2 NameError: name '_name_' is not defined
- Python 解决 :NameError: name 'reload' is not defined 问题
- python NameError: name 'raw_input' is not defined
- python name 'file' is not defined的解决办法
- Python编程中遇到NameError: name 'reload' is not defined 问题以及Python编码问题
- NameError: global name 'sp' is not defined