Python基础知识——文件操作和异常处理
2018-03-12 20:52
686 查看
文件操作是数据处理中的基础操作,包括读取、写入、存储/关闭等#文件不存在时自动创建
a=open('D:/1python学习/重新学习python/file.txt','w')
b='hello,python'
a.write(b)
a.write('\nhi,girl')
a.close()
#文件存在时
a=open('D:/1python/file2.txt','r')
b=a.read()
print(b)
line1=a.readline() #读取第一行
print(line1)
while True:
line=a.readline()
if len(line)==0:
break
print(line)
a.close()操作过程中出现异常会终止程序运行,有个异常处理之后,我们可以读取异常并继续运行程序#异常处理
print('python')
prints('python')
try:
print('python')
prints('python')
except Exception as error: #捕捉异常并存储为error
print(error)————来自韦玮老师课堂笔记
a=open('D:/1python学习/重新学习python/file.txt','w')
b='hello,python'
a.write(b)
a.write('\nhi,girl')
a.close()
#文件存在时
a=open('D:/1python/file2.txt','r')
b=a.read()
print(b)
line1=a.readline() #读取第一行
print(line1)
while True:
line=a.readline()
if len(line)==0:
break
print(line)
a.close()操作过程中出现异常会终止程序运行,有个异常处理之后,我们可以读取异常并继续运行程序#异常处理
print('python')
prints('python')
try:
print('python')
prints('python')
except Exception as error: #捕捉异常并存储为error
print(error)————来自韦玮老师课堂笔记
相关文章推荐
- 【脚本语言系列】关于Python基础知识文件操作,你需要知道的事
- python基础知识-文件操作
- 【python基础知识】文件操作中的读写模式讲解
- python-基础知识之文件操作
- Python学习之路-基础知识2-文件操作
- 基础知识(八)python文件操作相关函数
- Python基础知识4:文件操作
- python基础-文件操作
- python基础入门详解(文件输入/输出 内建类型 字典操作使用方法)
- 考考你的基础知识:C++ 文件操作ofstream、ifstream使用
- 用python处理文本,本地文件系统以及使用数据库的知识基础
- Python学习入门基础教程(learning Python)--5.6 Python读文件操作高级
- Python零碎知识(3):对文件的基本操作
- 12_Linux基础知识及命令――――使用通配符来操作文件
- [Python学习] 专题四.文件基础知识
- Python第二课 文件操作基础
- python操作MongoDB基础知识
- C++基础知识(四)文件的基本操作
- python 基础操作知识整理总结
- python基础知识-字符串的通用操作