1.异常
1.1.异常捕捉
try-except
py3异常信息捕捉用as e
try:
print("this is a try except demo")
open("noexist.txt",'r')
except IOError as e:
print("we find a error",e)
pass
1.2.捕捉多个异常
try:
print("this is a try except demo")
print(num)
open("noexist.txt",'r')
except (IOError,NameError) as e:
print("we find a error",e)
pass
1.3.finally
不管有没有捕捉到异常,执行finally
1.4.else
没有捕捉到异常,执行else
try:
num = 100
print(num)
except NameError as e:
print('产生错误了:%s'%e)
else:
print('没有捕获到异常,真高兴')
finally:
print('我一定会执行的哦')
1.5.抛出异常
raise
2.模块
类似java的引包
2.1 import
import 模块
from 模块 import 函数 : 导入模块中的 某个函数
2.2 模块发布
1.编写setup.py,py_modules需指明所需包含的py文件
from distutils.core import setup
setup(name="xwp", version="1.0", description="xwp's module", author="xingwenpeng", py_modules=['suba.aa', 'suba.bb', 'subb.cc', 'subb.dd'])
2.构建模块
python setup.py build
3.生成发布压缩包
python setup.py sdist
2.3 模块的安装和使用
python setup.py install
from 模块名 import 模块名或者*