600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Python文件处理和异常处理基础知识

Python文件处理和异常处理基础知识

时间:2022-02-03 20:12:59

相关推荐

Python文件处理和异常处理基础知识

B站视频:文件处理B站视频:异常处理

文件默认注释及文件改名:

(1)下面这些是File–>Setting设置里面的 coding and template 修改得到的 ‘’’

(2)改demo2.py名字:右键demo2.py–>找到Refactor–>找到rename == 点击即可改名

#-- coding = utf-8 --

#@Time:/11/6 16:40

#@Author:Yajing Wang

#@File: demo2.py

#@Software:PyCharm

文件处理

文件访问模式

文件操作

主要笔记记录如下:

#-*- coding = utf-8 -*-#@Time:/11/6 16:40#@Author:Yajing Wang#@File: demo2.py#@Software:PyCharm'''上面这些是File-->Setting设置里面的 coding and template 修改得到的 '''#改demo2.py名字:右键demo2.py-->找到Refactor-->找到rename == 点击即可改名f = open("dmeot.txt","w") #打开文件,若为“w”模式(写模式),文件不存在时新建f.write("hello world") #字符串写入demot.txt文件f.close()#关闭demot.txt文件f = open("dmeot.txt","r")#打开文件读操作#read方法开始时定位在文件头部,每执行一次,向后移动指定字符数content = f.read(5) #读f中的前五个单词出来,即为helloprint(content)content = f.read(5) #读f中的前五个单词出来,即为helloprint(content) #这里读出来的是“ worl”,不是总定位在开头f.close() #关闭文件f = open("dmeot.txt","a")#如果写利用“w”会把前面内容覆盖f.write("\nnihaoya")f.close()f = open("dmeot.txt","r")content = f.readlines()#一次性读取所有内容print(content)#逐行输出i=1for temp in content:text = f.readline()#逐行输出print(text)print("%d:%s"%(i,temp))i += 1f.close()#可以引入一个osimport os#os包里有很多文件读取,删除,重命名等函数

异常处理

'''#发生异常print("-------test-------1-")#能读出这一段#f = open("123.txt","r")#用只读模式打开文件发生异常print("-------test-------2-")#前面一句发生异常,不能读出print("==============================================================")''''''#捕获异常:try:print("-------test-------1-")f= open("123.txt","r")print("-------test-------2-")except IOError: #文件没找到属于io异常,即输入输出异常pass#捕获异常后所执行的代码print("==========================================")try:print(num)except NameError:#异常类型想要被捕获,需要类型一致(这里就不能IOError)print("you are wrong!")''''''#将上面两个捕获异常合起来可以这样写:try:print("-------test-------1-")f= open("123.txt","r")print("-------test-------2-")print(num)#(IOError,NameError)属于Exception里面,所有错误都可以用Exception表示except (IOError,NameError):#将所有可能长生的类型放入括号捕获print("you are wrong!")#获取错误描述:try:print("-------test-------1-")f= open("123.txt","r")print("-------test-------2-")print(num)except (IOError,NameError)as result:#将所有可能长生的类型放入括号捕获print("you are wrong!")print(result)'''#保障不管文件是否异常,都会关闭,便于下次对文件进行操作#try...catch...finally===不管异常与否,finally都会执行import timetry:f = open("dmeot.txt","r")try:while true:content = f.readline()if len(content) == 0:breaktime.sleep()print(content)finally:f.close()print("文件关闭")except Exception :print("you wrong !")

复制古诗作业:

#作业#写入古诗f = open("gushi.txt","w",encoding="utf-8")f.write("""青玉案·元夕辛弃疾东风夜放花千树。更吹落、星如雨。宝马雕车香满路。凤箫声动,玉壶光转,一夜鱼龙舞。蛾儿雪柳黄金缕。笑语盈盈暗香去。众里寻他千百度。蓦然回首,那人却在,灯火阑珊处。""")f.close()#显示古诗f = open("gushi.txt","r",encoding="utf-8")content = f.readlines()for temp in content:print(temp,end="")#复制古诗try:f = open("gushi.txt","r",encoding="utf-8")c = open("copy.txt", "w", encoding="utf-8")try:while True:content = f.readline()if len(content) == 0:breakc.writelines(content)finally:f.close()c.close()print("\n ok,it's done")except Exception as result:print("发生异常。。。")

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。