python FileError
>>> ls1=["nihia"]
>>> ls1
['nihia']
>>> ls1.pop()
'nihia'
>>> ls1.append("sssss")
>>> ls1
['sssss']
>>> assert len(ls1)>0
>>> ls1.pop()
'sssss'
>>> assert len(ls1)>0 Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
assert len(ls1)>0
AssertionError
>>>
>>> ls1.fishc Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
ls1.fishc
AttributeError: 'list' object has no attribute 'fishc'
>>>
>>> ms1[3] Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
ms1[3]
NameError: name 'ms1' is not defined
>>> ls1[3] Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
ls1[3]
IndexError: list index out of range
>>>
>>> dict1={'one':1 ,'two':2}
>>> dict1['one']
1
>>> dict1['four'] Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
dict1['four']
KeyError: 'four'
>>> dixt1.get('four') Traceback (most recent call last):
File "<pyshell#48>", line 1, in <module>
dixt1.get('four')
NameError: name 'dixt1' is not defined
>>> dict1.get('four')
>>> dict1.get('two')
2
>>> 1+'' Traceback (most recent call last):
File "<pyshell#51>", line 1, in <module>
1+''
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> 5/0 Traceback (most recent call last):
File "<pyshell#52>", line 1, in <module>
5/0
ZeroDivisionError: integer division or modulo by zero
>>>
>>>
>>>
>>>
>>> ================================ RESTART ================================
>>> Traceback (most recent call last):
File "/tmp/guest-WX48w4/文档/py1.py", line 2, in <module>
file_name=open('myname')
IOError: [Errno 2] No such file or directory: 'myname'
>>> ================================ RESTART ================================
>>> Traceback (most recent call last):
File "/tmp/guest-WX48w4/文档/py1.py", line 2, in <module>
file_name=open('myname.txt')
IOError: [Errno 2] No such file or directory: 'myname.txt'
>>> ================================ RESTART ================================
>>>
wrong
[Errno 2] No such file or directory: 'myname.txt'
>>> ================================ RESTART ================================
>>>
wrong
[Errno 2] No such file or directory: 'myname.txt'
>>> ================================ RESTART ================================
>>> Traceback (most recent call last):
File "/tmp/guest-WX48w4/文档/py1.py", line 2, in <module>
name=a+''
NameError: name 'a' is not defined
>>> ================================ RESTART ================================
>>>
wrong
unsupported operand type(s) for +: 'int' and 'str'
>>> ================================ RESTART ================================
>>> Traceback (most recent call last):
File "/tmp/guest-WX48w4/文档/py1.py", line 2, in <module>
int ('abc')
ValueError: invalid literal for int() with base 10: 'abc'
>>> ================================ RESTART ================================
>>> Traceback (most recent call last):
File "/tmp/guest-WX48w4/文档/py1.py", line 5, in <module>
print (file_name.read())
NameError: name 'file_name' is not defined
>>> 1/0 Traceback (most recent call last):
File "<pyshell#57>", line 1, in <module>
1/0
ZeroDivisionError: integer division or modulo by zero
>>> raise 1/0 Traceback (most recent call last):
File "<pyshell#58>", line 1, in <module>
raise 1/0
ZeroDivisionError: integer division or modulo by zero
>>>
try:
int ('')
#name=1+'1'
#file_name=open('myname.txt')
print (file_name.read())
file_name.close()
except IOError as reason: #eg1
print ('wrong\n'+str(reason))
except TypeError as reason:
print ('wrong\n'+str(reason))
except: #eg2
print ('wrong\n'+str(reason))
except (TypeError,IOError): #eg3
print ('wrong\n'+str(reason))
finally: #keep away from not closing the file
file_name.close()
python FileError的更多相关文章
- python click module for command line interface
Click Module(一) ----xiaojikuaipao The following mat ...
- python异常以及面向对象编程
一.面向对象 需要注意的是,在Python中,变量名类似__xxx__的,也就是以双下划线开头,并且以双下划线结尾的,是特殊变量,特殊变量是可以直接访问的,不是private变量,所以,不能用__na ...
- [Head First Python]4.读取文件datafile.txt, 去除两边空格, 存储到列表,从列表格式化(nester.py)后输出到文件man.out,other.out
datafile.txt #文件 Man: this is the right room for an argument. Other Man: I've told you once. Man: N ...
- Python学习的个人笔记(基础语法)
Python学习的个人笔记 题外话: 我是一个大二的计算机系的学生,这份python学习个人笔记是趁寒假这一周在慕课网,w3cschool,还有借鉴了一些博客,资料整理出来的,用于自己方便的时候查阅, ...
- python基础语法及知识点总结
本文转载于星过无痕的博客http://www.cnblogs.com/linxiangpeng/p/6403991.html 在此表达对原创作者的感激之情,多谢星过无痕的分享!谢谢! Python学习 ...
- Python错误和异常 学习笔记
错误和异常概念 错误: 1.语法错误:代码不符合解释器或者编译器语法 2.逻辑错误:不完整或者不合法输入或者计算出现问题 异常:执行过程中出现万体导致程序无法执行 1.程序遇到 ...
- Python中的多进程与多线程(一)
一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
- Python 小而美的函数
python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况 any any(iterable) ...
随机推荐
- one to many
@OneToMany(mappedBy="order",fetch=FetchType.EAGER,cascade={CascadeType.ALL}) private Li ...
- jenkins 入门教程(上)
jenkins是一个广泛用于持续构建的可视化web工具,持续构建说得更直白点,就是各种项目的"自动化"编译.打包.分发部署.jenkins可以很好的支持各种语言(比如:java, ...
- bzoj 3163: [Heoi2013]Eden的新背包问题
Description "寄没有地址的信,这样的情绪有种距离,你放着谁的歌曲,是怎样的心心静,能不能说给我听."失忆的Eden总想努力地回忆起过去,然而总是只能清晰地记得那种思念的 ...
- 让游戏以高性能GPU(独立显卡)运行
在EXE中导出全局变量: N卡: extern "C" { __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001 ...
- 了解 JS 原型
原型概念 当创建了一个函数时,就会根据一组特定的规则为该函数创建一个 prototype 属性,这个属性指向函数的原型对象.在默认情况下,所有原型对象都会自动获得一个constructor 的属性 这 ...
- 我为什么还要造轮子?欠踹?Monk.UI表单美化插件诞生记!
背景 目前市场上有很多表单美化的UI,做的都挺不错,但是他们都有一个共同点,那就是90%以上都是前端工程师开发的,导致我们引入这些UI的时候,很难和程序绑定.所以作为程序员的我,下了一个决定!我要自己 ...
- mysql Workbench 执行删除命令
SET SQL_SAFE_UPDATES = 0;delete from table1; SET SQL_SAFE_UPDATES = 1;
- redis-删除所有key
删除所有Key,可以使用Redis的flushdb和flushall命令 //删除当前数据库中的所有Key flushdb //删除所有数据库中的key flushall 如果要访问 Redis 中特 ...
- Linux 查找文件并删除文件内容
find * ./ |while read file; do echo ''>$file; done
- virtual和abstract
virtual和abstract都是用来修饰父类的,通过覆盖父类的定义,让子类重新定义. 共同点:如果用来修饰方法,前面必须添加public,要不然就会出现编译错误:虚拟方法或抽象方法是不能私有的. ...