【今天是python基础的最后一节,明天开始学习python的项目工程】
一、读写文件
读写文件的主方法为 open(),它包含两个实参。
第一个是需要打开的文件名称,可使用相对路径或绝对路径;
第二个是选择的模式,默认情况为 'r'(读取模式)。
在写文件时要传入'w'或者'a'进行文件修改。写文件中两者的区别在于:
w模式下是覆盖原文件来更新写入内容,而a模式下是在原文件后面添加内容。
#9.1 读文件
with open('pi_digits.txt') as file_object:
contents = file_object.read()
print(contents)
#9.1.1 路径
#9.1.1.1 py文件下子文件夹的文件
with open('test_files\pi_digits.txt') as file_object:
contents = file_object.read()
print(contents)
#9.1.1.2 绝对路径(注意文件中如有\t,注意转义符)
file_path = 'E:\python_work\pythonProject\\test_files\pi_digits.txt'
with open(file_path) as file_object:
contents = file_object.read()
print(contents)
#9.1.2 逐行读取
filename = 'pi_digits.txt'
with open(filename) as file_object:
for line in file_object:
print(line)
#9.1.3 一个包含文件内容的列表
with open(filename) as file_object:
lines = file_object.readlines()
for line in lines:
print(line.rstrip())
#9.1.4 文件内容的使用
with open(filename) as file_object:
lines = file_object.readlines()
pi_string = ''
for line in lines:
pi_string +=line.strip()
print(pi_string)
print(len(pi_string))
#9.2 写文件
filename ='programming.txt'
with open(filename,'w') as file_object:
file_object.write("I love python.\n")
#9.2.1 写入多行: \n
#9.2.2 附加到文件
with open(filename,'a') as file_object:
file_object.write("I also love finding mening in large datastes.\n")
二、异常处理
异常处理的三种情况:
出现异常, 并且需要提示警告 | 但如果没有出现异常, 后续还有正常模块 | 出现异常, 但不想处理 |
try: except: | try: except: else: | try: except: pass |
#9.3 异常
#9.3.1 出现异常,希望提示警告:try...except
#9.3.2 没有异常:try...except...else
num1 = input("please input a number:")
num2 = input("enter another number:")
try:
answer = int(num1)/int(num2)
except ZeroDivisionError:
print("It's an error.")
else:
print(answer)
#9.3.3 出现异常,希望忽略:try...except pass
try:
print(5/0)
except:
pass
三、测试
包括测试函数、测试类。注意:这里是指被测试的对象。
而测试工具一般是一个名字带有Test的类,其继承unittest.TestCase。
如NameTestCase、TestAnonymousSurvey.
主要测试方法需要查阅unittest.TestCase类中的断言方法,如assertEqual、assertIn等。
最后,setUp()方法为测试类提供便捷,一次实例,一直使用。
#10.1 测试函数
import unittest
from name_function import get_formatted_name
class NameTestCase(unittest.TestCase):
def test_first_last_name(self):
formatted_name = get_formatted_name('janis','joplin')
self.assertEqual(formatted_name,'Janis Joplin')
def first_last_middle_name(self):
formatted_name = get_formatted_name('wol','ajsa','dadd')
self.assertEqual(formatted_name,'Wol Dadd Ajsa')
unittest.main()
#10.2 测试类
import unittest
from survey import AnonymousSurvey
class TestAnonymousSurvey(unittest.TestCase):
def test_store_single_response(self):
question = "What language did you first learn to speak?"
my_survey = AnonymousSurvey(question)
my_survey.store_response('English')
self.assertIn('English',my_survey.responses)
def test_store_three_response(self):
question = "What language did you first learn to speak?"
my_survey = AnonymousSurvey(question)
responses = ['English','Spanish','Mandarin']
for response in responses:
my_survey.store_response(response)
for response in responses:
self.assertIn(response,my_survey.responses)
unittest.main()
#10.2.1 方法setUp()
class TestAnonymousSurvey(unittest.TestCase):
"""实例化一次对象即可"""
def setUp(self):
question = "What language did you first learn to speak?"
self.my_survey = AnonymousSurvey(question)
self.responses = ['English','Spanish','Mandarin']
参考
Python编程:从入门到实践S