Python-25 with
来源:互联网 发布:tensorflow函数 编辑:程序博客网 时间:2024/05/29 14:57
丰富的else语句
else不仅可以和if搭配使用,还可以和for、while搭配使用。
else还可以和异常处理搭配使用。
try: print(int('123'))except ValueError as reason: print('出错了:'+str(reason))else: print('没有异常')
>>> ================================ RESTART ================================>>> 123没有异常>>>
with语句
打开文件又要关闭,还要关注异常处理有点烦人,所以Python提供了一个with语句,利用这个语句抽象出文件操作中频繁使用的try/except/finally相关细节。
对文件使用with语句,将可以大大减少你的代码量,而且在也不用担心文件打开忘记关闭的尴尬了(with会自动帮助你关闭文件)。
try: with open('E:\\record.txt','r') as f: for each_line in f: print(each_line)except OSError as reason: print('出错了:'+ str(reason))
阅读全文
0 0
- Python-25 with
- python with
- Python with
- python with
- Python With
- python with
- Python With
- python with
- python with
- python~with
- python with
- Python with
- [Python]Apache with Mod_python
- Java with Python
- 【LeetCode with Python】 Permutations
- Starting Out with Python
- XML Processing with Python
- python with...as...用法
- spring源码学习之路---深度分析IOC容器初始化过程(四)
- [刷题]ACM ICPC 2015合肥网络赛
- Kay and Snowflake CodeForces
- pandas入门——多表操作
- okhttp添加本地缓存(支持get post)
- Python-25 with
- python笔记之unittest
- BZOJ 4627回转寿司(值域线段树)
- 我的spring 上
- 设计作品的好坏怎么看
- java多线程----synchronized方法锁能否锁住对象呢?
- php flush类输出缓冲剖析
- 连接IBM MQ原因码报2035的错误解决方法
- 还不懂什么叫T型UI设计师你就OUT啦