'''文件异常的处理,计算文件夹中文本的字数'''
def word_count(file_name):
try:
with open(file_name) as test:
lines = test.read()
except FileNotFoundError:
print ("The file doesn't exist!")
else:
word = lines.split()
nu = len(word)
print("The file has "+str(nu)+" words.")
path = '/mnt/hgfs/gongxiang/wonderland.txt'
word_count(path)
'''除法计算器关于两个异常的处理'''
while True:
print ("Give me two number and i will divede them.")
print("Enter 'q' to quit!")
first_num =input ("Enter the first number:")
if first_num == 'q':
print("Thank you for your use!")
break
second_num = input ("Enter the second number:")
if second_num == 'q':
print("Thank you for your use!")
break
try:
ans = int(first_num)/int(second_num)
except ValueError:
print("Character can't do caculate!")
confirm = input("Contineue or enter 'q' to quit!")
if confirm == 'q':
print("Thank you for your use!")
break
except ZeroDivisionError:
print ("You can't divide 0!")
confirm = input("Contineue or enter 'q' to quit!")
if confirm == 'q':
print("Thank you for your use!")
break
else:
print ("The answer is: "+str(ans))
confirm = input("Contineue or enter 'q' to quit!")
if confirm == 'q':
print("Thank you for your use!")
break
'''文件内容的读出'''
import json
secentence = '张旭草泥马'
filename = '/mnt/hgfs/gongxiang/zx.json'
with open(filename,'w')as f_obj:
json.dump(secentence,f_obj)
'''文件的写入'''
import json
filename = '/mnt/hgfs/gongxiang/zx.json'
with open(filename)as f_obj:
secentence = json.load(f_obj)
print (secentence)
'''测试'''
import unittest
from name import get_name_formatted
class NameTest(unittest.TestCase):
def test_name(self):
formatted_name = get_name_formatted('calvin','harris')
self.assertEqual(formatted_name,'Calvin Harris')
unittest.main()
''''被测试文件'''
def get_name_formatted(first_name,second_name,middle_name=''):
if middle_name:
print(first_name.title()+' '+middle_name.title()+' '+second_name.title())
else:
print(first_name.title()+' '+second_name.title())
python第十二天
猜你喜欢
转载自blog.csdn.net/CalvinHARRIS/article/details/82859224
今日推荐
周排行