File operations and exception handling in python

File introduction:
AVI BMP JPEG MAX MOV Movie MP3 MPEG Music OneNote HTML Divx Outlook PDF RAR RM RTF DLL TXT Visio VOB WAV WMA Word ZIP File HTML FLV HTML ISO PSD
file function Stored in storage, the program can be used directly when the program is executed next time, without having to make a new copy, saving time and effort

Open and close the file
Steps: New - Write data - Close
Open the word software,
create a new word file,
write data,
save,
close
Conclusion
Open the file, or create a new file
Read/Write
Close the data

open a file

在python使用,使用open函数,可以打开一个已经存在的文件,或者是新建文件open(文件名,访问模式)
格式

f = open('文件','w')或者f=open('文件','r')
r:只读 打开文件。文件的指针放在文件的开头,这是默认模式。
w:写入 打开文件写入,如果该文件已经有内容则覆盖,如果没有该文件则新建
a:写入 打开文件写入,如果文件已经存在光标则在文件最后则追加
rb:以二进制格式打开一个文件用于只读,文件指针将会放在文件的开头(默认)
wb:以二进制格式打开一个文件只用于手写输入,如果该文件已存在则将覆盖如果该文件不存在创建新文件。
ab:用于二进制格式文件写入,如果文件已存在,文件指针将会放在文件的结尾,也就是说新写入的内容最后,如果不存在该文件,创建新文件进行写入。

write data

格式
对象=open(“文件”,w)
对象.write(“写入数据”)
对象.close

insert image description here
insert image description here
Append write to
insert image description here
view the result If
insert image description here
successful, it will overwrite the previously written result.
If the file does not exist, create it. If it exists, it will be emptied first, and then the data will be written.

读数据 以读的方式打开文件 读数据
格式
	对象=open("文件",r)
	变量=对象.read()
	print(变量)
如果用open打开文件时,如果使用的"r",那么可以省略,即只写 open('test.txt')
如果没有文件,打开报错,存在该文件才能操作
如果文件中存在中文显示,会出现乱码需要添加encoding='utf-8'
open(‘test.txt’,”r”, encoding='utf-8')

insert image description here

读数据
就像read没有参数时一样,readlines可以按照行的方式把整个文件中的内容进行一次性读取,并且返
回的是一个列表,其中每一行的数据为一个元素
格式:
对象 = open("文件",r)
变量 = 对象.readlines()
print(变量)

insert image description here

可写
格式
	对象 = open("",a)
	对象.write("写入数据"
	对象.close
	
	f = open("test.txt",a)
	f.write("新的数据"
	对象.close

read data

格式1
对象 = open("二进制文件",rb)
变量 = 对象.read()
print(变量)

with open("二进制文件","rb")as 对象:
	变量=对象.read()
	print(变量)

insert image description here
The second way to get binary
insert image description here

with open("二进制文件","wb") as 对象:
	变量 = 对象.write()
	print(变量)

insert image description here
Close the file:
format
close()

文件和文件夹的操作
	os模块中rename()可以完成对文件的重命名操作
格式
	import os
	os.rename("需要修改的文件名""新文件名")

insert image description here

文件夹的相关操作
创建文件夹
os模块中mkdir()可以完成对文件的重命名的操作
获取当前目录
os模块中的getcwd()可以获取当前目录
异常
异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行。
一般情况下,在Python无法正常处理程序时就会发生一个异常。
异常是Python对象,表示一个错误。
当Python脚本发生异常时我们需要捕获处理它,否则程序会终止执行。

insert image description here
exception handling

捕捉异常可以使用try/except语句。
try/except语句用来检测try语句块中的错误,从而让except语句捕获异常信息并处理。
如果你不想在异常发生时结束你的程序,只需在try里捕获它。
格式
try:
<语句> #运行别的代码
except <名字><语句> #如果在try部份引发了'name'异常
else:
<语句> #如果没有异常发生

insert image description here

Use except without any exception type

格式
try:
	正常的操作
except :
	发生异常,执行这块代码
else:
	如果没有异常执行这块代码
以上方式try-except语句捕获所有发生的异常。但这不是一个很好的方式,我们不能通过该程序识别
出具体的异常信息。因为它捕获所有的异常。

insert image description here
Using except with multiple exception types

try:
	异常的操作
except(Exception1[, Exception2[,...ExceptionN]]]):
	发生以上多个异常中的一个,执行这块代码
	......................
else:
	如果没有异常执行这块代码
	成功将异常打出

insert image description here

try-finally 语句 语句无论是否发生异常都将执行最后的代码
try:
	<语句>
finally:
	<语句> #退出try时总会执行
当在try块中抛出一个异常,立即执行finally块代码。
finally块中的所有语句执行后,异常被再次触发,并执行except块代码。
参数的内容不同于异常

insert image description here
exception delivery
insert image description here

触发异常
可以使用raise语句自己触发异常
```![在这里插入图片描述](https://img-blog.csdnimg.cn/cca6963a730940709e766732e79fa604.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Yav5pmo6Iq4,size_20,color_FFFFFF,t_70,g_se,x_16)

```bash
用户自定义异常
定义
通过创建一个新的异常类,程序可以命名它们自己的异常。异常应该是典型的继承自Exception类,通过直接
或间接的方式

insert image description here
module

Python 模块(Module),是一个Python文件,以.py 结尾,包含了Python 对象定义和Python语句

insert image description here

模块的引入
impport 文件名

insert image description here
square root
insert image description here

from…import 语句
from modname import name1[, name2[, ... nameN]]

insert image description here

Guess you like

Origin blog.csdn.net/weixin_44826661/article/details/124126566