python//Jan.18th,2020//异常

Jan.18th,2020
##异常

#处理ZeroDivisionError异常
##trackback   引用,引用功能
#print(5/0)

#使用try_except代码块
#try:
#    print(5/0)
#except ZeroDivisionError:
#    print("You can't divide by zero!")

#使用异常避免崩溃
#print("Give me two number,and I will divide them.")
#print("Enter 'q'to quit.")
#while True:
#    first_number=input("\nFirst number:")
#    if first_number=='q':
#        break
#    second_number=input("\nSecond number:")
#    if second_number=='q':
#        break
#    answer=int(first_number)/int(second_number)
#    print(answer)
          ##输入5  0  发生异常

#else代码块
#print("Give me two number,and I will divide them.")
#print("Enter 'q'to quit.")
#while True:
#    first_number=input("\nFirst number:")
#    if first_number=='q':
#        break
#    second_number=input("\nSecond number:")
#    try:
#        answer = int(first_number) / int(second_number)
#    except ZeroDivisionError:
#        print("You can't divide by 0")
#    else:
#        print(answer)

#处理FileNotFoundError异常
##读取不存在文件
#file_name='s.txt'
#with open(file_name) as f_obj:
#    contents=f_obj.read()

#filename='s.txt'
#try:
#    with open(filename) as f_obj:
#        contents = f_obj.read()
#except FileNotFoundError:
#    print("Sorry,the file "+filename+" does not exist")

#分析文本
#title="I LOVE CXK"
#print(title.split())    #split() 根据 一个字符串创建一个单词列表

#filename=r'C:\Users\Administrator\Desktop\idocdown3210\Fish-v3210-1212\kpdf\初中英语书单词汇总(初中全部).txt'
#try:
#    with open(filename) as f_obj:
#        contents = f_obj.read()
#except FileNotFoundError:
#    print("Sorry,the file "+filename+" does not exist")
#else:
#    words = contents.split()
#    print(words)
#    num_words = len(words)
#    print(num_words)

#使用多个文件
#对多本书进行分析
#def count_words(filename):
#    try:
#        with open(filename) as f_obj:
#            contents=f_obj.read()
#    except FileNotFoundError:
#        msg="Sorry,the file "+filename+" does not exist."
#    else:
#        words=contents.split()
#        num_words=len(words)
#        print("The file "+filename+" has about "+str(num_words)+" words.")
#filename1=r'C:\Users\Administrator\Desktop\idocdown3210\Fish-v3210-1212\kpdf\初中英语书单词汇总(初中全部).txt'
#filename2=r'C:\Users\Administrator\Desktop\idocdown3210\Fish-v3210-1212\kpdf\圆周率小数点后100万位.txt'
#count_words(filename1)
#count_words(filename2)

#pass语句告诉python什么都不要做
#def count_words(filename):
#    try:
#        with open(filename) as f_obj:
#            contents=f_obj.read()
#    except FileNotFoundError:
#        pass
#    else:
#        words=contents.split()
#        num_words=len(words)
#        print("The file "+filename+" has about "+str(num_words)+" words.")
#filename1=r'C:\Users\Admini单词汇总(初中全部).txt'
#filename2=r'C:\Users\Administrator\Desktop\idocdown3210\Fish-v3210-1212\kpdf\圆周率小数点后100万位.txt'
#count_words(filename1)
#count_words(filename2)#







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值