码迷,mamicode.com
首页 > 编程语言 > 详细

Python学习——文件操作和异常处理

时间:2018-06-27 00:18:35      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:style   ali   学习   not   first   except   and   word   ext   


# title = "Alice in Wonderland"
# print(title.split())


def count_words(filename):
‘‘‘ count how many words in a text file ‘‘‘
try:
with open(filename) as f_obj:
contents = f_obj.read()
except FileNotFoundError:
pass
#msg = "Sorry, the file " + filename + " does not exist."
#print(msg)
else:
words = contents.split()
print("The file " + filename + " has about " + str(len(words)) + " words.")


def addTwoNumber():
‘‘‘ add two numbers. ‘‘‘
first_number = input("First Number: ")
second_number = input("Second Number: ")
try:
first_number = int(first_number)
second_number = int(second_number)
except ValueError:
print("Please check if your input is legal.")
return False
else:
res = first_number + second_number
print(str(first_number)+" + "+str(second_number)+" is "+str(res))
return True

Python学习——文件操作和异常处理

标签:style   ali   学习   not   first   except   and   word   ext   

原文地址:https://www.cnblogs.com/QiLF/p/9231787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!