学习了对于文件解决以及代码测试的内容
以下所用文件能够在 “图灵社区” 收费下载


文件解决局部

# 建设相对路径的办法如下:# file_path = 'C:\Users\other_files\text_files\filename.txt'# with open(file_path) as file_objectwith open('text_file\pi_digits.txt') as file_object:  # 传递绝对路径    contents = file_object.read()    print(contents)    # 文末会有1个空行,因为read()达到文件开端时返回一个空字符串    print(contents.rstrip())# 逐行读取filename = 'text_file\pi_digits.txt'with open(filename) as file_object:    for line in file_object:        print(line)with open(filename) as file_object:    lines = file_object.readlines()  # 读取每一行并存在一个列表中for line in lines:    print(line.rstrip())pi_string = ''for line in lines:    pi_string += line.rstrip()print(pi_string)  # 右边也有空格print(len(pi_string))pi_string = ''for line in lines:    pi_string += line.strip()print(pi_string)print(len(pi_string))

输入后果为:
3.1415926535
8979323846
2643383279

3.1415926535
8979323846
2643383279
3.1415926535

8979323846

2643383279

3.1415926535
8979323846
2643383279
3.1415926535 8979323846 2643383279
36
3.141592653589793238462643383279
32


检测生日是否在圆周率前百万位
这部分有须要输出的内容,故不展现输入

filename = 'text_file\pi_million_digits.txt'with open(filename) as file_object:    lines = file_object.readlines()  # 读取每一行并存在一个列表中pi_string = ''for line in lines:    pi_string += line.strip()print(pi_string[:52] + "...")print(len(pi_string))birthday = input("Enter your birthday, in the form mmddyy:")if birthday in pi_string:    print("Your birthday appears in the first million digits of pi!")else:    print("Failed.")

文件写入及读取局部

# 写入空文件filename = 'programming.txt'with open(filename,'w') as file_object:    # 以写入的形式关上文件,若无文件将创立文件    file_object.write("I love programming.\n")    file_object.write("I love creating new games.\n")    # 每次输出都会笼罩本来的内容# 以追加的形式关上with open(filename,'a') as file_object:    file_object.write("I love finding meaning in large database.\n")    file_object.write("I love creating apps that run in a browser.\n")# 应用try-except代码块解决异样try:    print(5/0)except ZeroDivisionError:    print("You can't divide by zero!")    # 并非真正的解决,而是以不报错的形式持续运行while True:    first_num = input("\nFirst number:")    if first_num == 'q':        break    second_num = input("\nSecond number:")    try:        answer = int(first_num)/int(second_num)    except ZeroDivisionError:        print("You can't divide by zero!")    else:        print(answer)# 失败时一声不吭:应用pass命令# 应用json模块存储数据import jsonnumbers = [2,3,5,7,11,13]filename = 'number.json'with open(filename,'w') as f_obj:    json.dump(numbers,f_obj)with open(filename) as f_obj:     new_numbers = json.load(f_obj)print(new_numbers)

函数测试局部

def get_formatted_name(first, last, middle=''):    if middle:        full_name = first + ' ' + middle + ' ' +last    else:        full_name = first + ' ' +last    return full_name.title()class NamesCase(unittest.TestCase):    def test_first_last_name(self):        formatted_name = get_formatted_name('janis', 'joplin')        self.assertEqual(formatted_name, 'Janis Joplin')    def test_first_middle_last_name(self):        formatted_name = get_formatted_name('janis', 'joplin', 'stupid')        self.assertEqual(formatted_name, 'Janis Stupid Joplin')# 各种断言形式    # assertEqual    # assertNotEqual    # assertTrue:判断是否为真    # assertFalse    # assertIn:判断是否在列表中    # assertNotIn# 办法setup()    # 用以创立一个考察对象,一个答案列表