本文主要是介绍Python基础学习day10,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
'''print('step0')
try:
print('step1')
a = 3/0
print('step2')
except BaseException as e:
print('step3')
print('e')
print(type(e))
print('end!!!')'''
'''while True:
try:
x = int(input('请输入一个数字'))
print('输入数字:',x)
if x==88:
print('退出程序')
break
except BaseException as e:
print(e)
print('异常,输入的不是一个数字')
print('循环数字输入程序结束!')'''
# 测试try...多个except结构
'''try:
a = input('请输入一个被除数')
b = input('请输入一个除数')
c = float(a)/float(b)
print(c)
except ZeroDivisionError:
print('异常,不能除以0')
except ValueError:
print('异常,不能将字符串转化成数字')
except NameError:
print('异常,变量不存在')
except BaseException as e:
print(e)'''
'''try:
a = input('请输入一个被除数')
b = input('请输入一个除数')
c= float(a)/(b)
except BaseException as e:
print(e)
else:
print(c)
finally:
print('我是finall中的语句,无论发生异常与否,都执行!')
print('程序结束!')'''
# 测试finally
'''f = open('d:/a.txt','r')
content = f.readline()
print(content)
f.close()'''
'''with open('d:/a.txt','r')as f:
content = f.readline()
print(content)
print('程序执行结束')'''
# 测试trackback模块
'''import traceback
try:
print('step1')
num = 1/0
except:
traceback.print_exc()
########异常信息输出到指定文件################
try:
print('step1')
num = 1/0
except:
with open ('d:/a.txt','a')as f:
traceback.print_exc(file=f)'''
'''# 测试自定义异常
class AgeError(Exception): # 继承Exception
def __init__(self,errorInfo):
Exception.__init__(self)
self.errorInfo = errorInfo
def __str__(self):
return "年龄错诶!"
#################测试代码#####################
if __name__=='__main__': # 如果为Ture,则模块是作为独立文件运行,可以执行测试代码
age = int(input("输入一个年龄:"))
if age<1 or age>150:
raise AgeError(age)
else:
print('正常年龄:',age)'''
# 测试调式、断点
'''def aa():
print('run in aa() start')
print('step1')
num1 = 3
num2 = num1*4
num3 = num2
print('step2')
print('run in aa() end!!!')
if __name__=='__main__':
print('main:step1')
aa()
print('main:step2')
print('main:end!!!')'''
# 使用异常机制管理文件对象的关闭操作
'''try:
f = open(r'c.txt','w')
strs = ['aa','bb','cc']
f.writelines(strs)
finally:
f.close()'''
'''with open(r'd.txt','a') as f:
f.write('gaoqi,i love u!')
import os
import os.path
print(os.path.isabs('d:/a.txt'))'''
# 测试递归算法
def a1():
print('a1')
a1()
def b1():
print('b1')
a1()
# 打印所有的目录和文件
import os
def getAllFiles(path):
childFiles = os.listdir(path)
for file in childFiles:
print(file)
getAllFiles('test_os')
这篇关于Python基础学习day10的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!