Files and Exceptions
来源:互联网 发布:软件架构图 编辑:程序博客网 时间:2024/05/22 15:02
Python中基本输入机制是line based(“基于行的”,不知道翻译的对不对),即当一个文件打开后,Python将数据视为一个一个单独的行,配合for循环等循环语句实现input操作
import os #导入os module
os.getcwd() #获取当前工作目录 cwd=current working directory
os.chdir() #改变目录 chdir= change directory
data = open(‘sketch.txt’) #open()打开一个文件,返回一个文件对象
print(data.readline(), end=’ ‘)
print(data.readline(), end=’ ‘)#readline() method读取文件中的一行,并且自动会切换到下一行
data.seek(0) #将文件重新定位到开头
for each_line in data:
print(each_line, end=’’) #循环按行打印
data.close()#关闭文件
str.split(sep = None, maxsplit = -1) #以sep为分界符,生成一个list存储分界符两侧数据
详细用法参考官方文档
str.find(substr) #在str中查找字串,如果存在返回字串索引位置,否则返回-1
遇到异常情况有两种做法,第一种添加额外的逻辑结构,但是这是不可靠的,如果数据结构改变,就需要添加更多的逻辑结构
In python’s world, runtime errors are called exceptions
list有两种 一种是可改变的 一种是不可改变的
不可改变的list称作tuple
pass 语句 相当于NULL,不执行,起占位作用
try-except statement
如果出现异常执行except
os.path.exists(‘filename’)
检测是否存在filename
多用exception handling mechanism
可以通过指定异常类型来处理不同的异常
try:
code#不变
except ValueError:
code#出现ValueError时异常处理代码
except IOError:
code#出现IOError 时异常处理代码
- Files and Exceptions
- Files and Exceptions: Dealing with Errors
- Transactions and Exceptions
- Exceptions and control flow
- SEH and C++ Exceptions
- Errors and Exceptions
- Python Errors and Exceptions
- [OS]Exceptions and Interrupts
- Python Errors and Exceptions
- Throwing and Catching Exceptions
- 10 Exceptions and Control
- Exceptions in Java and C#
- Python[十六]:Class and exceptions
- Interrupts, hardware exceptions, and signals
- C++ Exceptions: Pros and Cons
- Handling unhandled exceptions and signals
- Handling unhandled exceptions and signals
- TBB之Exceptions and Cancellation
- Yii PHP 框架分析(三)
- 拯救“大兵”酒吧--空间加香
- 在360工作的这几天
- 关于httpwatch的问题
- JavaScript,Jsp与jQuery区别介绍
- Files and Exceptions
- 栈的C语言实现源码
- SAP应用事务开发—TABLE CONTROL 控件之应用(一)
- 笔试面试总结(1)
- android 包名解析错误的问题
- 三极管的电平转换及驱动电路分析
- Oracle VM 3序列 安装尝鲜
- 2014年Android碎片化报
- Flex设置TextInput的默认焦点、切换焦点的例子