17.Python
来源:互联网 发布:查身份证真假软件 编辑:程序博客网 时间:2024/06/05 17:44
输出错误:try:, except ... as ...: 看如下代码
try:
file=open('eeee.txt','r') #会报错的代码
except Exception as e: # 将报错存储在 e 中
print(e)
"""
[Errno 2] No such file or directory: 'eeee.txt'
"""
处理错误:会使用到循环语句。首先报错:没有这样的文件No such file or directory.然后决定是否输入y,输入y以后,系统就会新建一个文件(要用写入的类型),再次运行后,文件中就会写入ssss
try:
file=open('eeee.txt','r+')
except Exception as e:
print(e)
response = input('do you want to create a new file:')
if response=='y':
file=open('eeee.txt','w')
else:
pass
else:
file.write('ssss')
file.close()
"""
[Errno 2] No such file or directory: 'eeee.txt'
do you want to create a new file:y
ssss #eeee.txt中会写入'ssss'
阅读全文
0 0
- 17.Python
- Python
- Python
- Python
- python
- Python
- PYTHON
- Python
- Python
- Python
- Python
- Python
- Python
- Python
- Python
- Python
- Python
- python
- 关于嵌套类与外围类的详细解释
- mt6735[Call] USSD是否支持USSD Phase 1 & 2 2
- Spring boot + Maven环境搭建comet4j前端页面推送
- c#导入导出Excel
- vipkid创始人米雯娟传奇:高二退学,创业逆袭成50亿教育巨头
- 17.Python
- 乐变热更新后台的使用
- Docker1.12.1之swarm集群搭建与使用
- Android使用Http访问网络
- 7、HTML5元素
- Html之路
- idea bookmark
- 0基础新手入门linux系统的四个建议
- EasyUi中datagrid使用心得