异常捕获
try:
f = open("./abc.txt","r",encoding="UTF-8")
except:
print("==========")
f = open("./abc.txt","w",encoding="UTF-8")
#指定异常
try:
print(xxx_test)
except NameError as e:
print(f"error={e}")
#多个异常
try:
#print(name)
num = 10
result = num/0
except (NameError,ZeroDivisionError) as e:
print(f"tuple=error={type(e)}")
#捕获所有异常
try:
10/0
print(xxxxxxxx)
except Exception as e:
print(f"all=error={type(e)}")
#无异常处理
try:
print("hello world python")
except Exception as e:
print(f"tuple=error={type(e)}")
else:
print("no exception")
#不管是否有异常都要执行的
try:
print("hello world python")
except Exception as e:
print(f"tuple=error={type(e)}")
else:
print("no exception")
finally:
print("xxxxxxxxxxxxx")
异常传递性
模块
自定义模块![](https://i-blog.csdnimg.cn/blog_migrate/59b71197f4f95109461cf23f7ec5d61e.png)
引入不同模块下相同的函数时候 后边引入的会把前边引入的覆盖掉
模块测试
def sum(a,b):
return a + b
#__main__
if __name__ == '__main__':
result = sum(23,45)
print(result)
包
#from time import sleep
import time
import module01
import package01.moudle01 as mu
from package01 import moudle02
result = module01.sum(123,456)
print(result)
result = mu.mulity(22,33)
print(result)
moudle02.say_say("我说 你输出")
#time.sleep(10)
导入三方包
1.在需要的文件中导入包名
1. 使用pip install (pip 包管理工具 类似 iOS 的pod)