#练习 10-1:
# 1
# file_name = 'D:/whatever/lib/Learning_python.txt'
# with open(file_name) as file:
# contents = file.read()
# print(contents.rstrip())
# 2
# file_name = 'D:/whatever/lib/Learning_python.txt'
# with open(file_name) as file:
# for line in file:
# print(line.rstrip())
# 3
# file_name = 'D:/whatever/lib/Learning_python.txt'
# with open(file_name) as file:
# lines = file.readlines() #构造一个每个元素是行的列表
# #此时置于with代码块外面依旧能用
# for line in lines:
# print(line.rstrip())
# 练习 10-2:
# file_name = 'D:/whatever/lib/Learning_python.txt'
# with open(file_name) as file:
# lines = file.readlines()
# for line in lines:
# new = line.replace('Python', 'bilibili')
# print(new.strip())
# 练习 10-3:
# 此处有问题 但不知道如何解决
# file_name = 'D:/whatever/lib/guest.txt'
# def open_file():
# """用于打开文档"""
# with open(file_name) as files:
# lines = files.readlines()
# for line in lines:
# print(line.rstrip())
# def Check_duplicate_file():
# """用于查重名字"""
# with open(file_name) as files:
# linens = files.readlines()
# sign = True
# count = 0
# while sign == True:
# name = input('Please enter your name:')
# for line in linens:
# if line == name:
# info = 'You have already entered this name\n'
# info += 'Try to create a new one'
# print(info)
# break
# count += 1
# if count == len(linens):
# sign == False
# with open(file_name,'a') as file:
# file.write(name)
# while True:
# Check_duplicate_file()
# mes = input('Do you wanna know the list of guest now?(yes/no)\n')
# if mes == 'yes':
# open_file()
# message = input('Ok,is there anyone left?(yes/no)\n')
# if message == 'no':
# break
# 练习 10-4:
# file_name = 'D:/whatever/lib/guest_book.txt'
# while True:
# name = input('please enter your name:')
# print('welcome to happy club!!')
# info = f'{name} has been there\n'
# with open(file_name,'a') as file:
# file.write(info)
# message = input('Ok,is there anyone left?(yes/no)\n')
# if message == 'no':
# break
# 练习 10-5:
# file_name = 'D:/whatever/lib/guest_reason.txt'
# while True:
# reason = input('please enter your reason that you like code:')
# with open(file_name,'a') as file:
# file.write(reason)
# message = input('Ok,is there anyone reason?(yes/no)\n')
# if message == 'no':
# break
# 练习 10-6:
# info = 'Try to give me two number and i will divide them\n'
# print(info)
# first_number = input('first_number:')
# second_number = input('second_number:')
# try:
# answer = int(first_number)+int(second_number)
# except ValueError:
# print('please use digital rather than text!')
# else:
# print(answer)
# 练习 10-7:
# info = 'Try to give me two number and i will divide them\n'
# info += '(input q to exit at any time)'
# print(info)
# while True:
# first_number = input('first_number:')
# if first_number == 'q':
# break
# second_number = input('second_number:')
# if second_number == 'q':
# break
# try:
# answer = int(first_number)+int(second_number)
# except ValueError:
# print('please use digital rather than text!')
# else:
# print(answer)
# 练习 10-8:
# file_name_1 = 'D:/1/lib/cats.txt'
# file_name_2 = 'D:/whatever/lib/dogs.txt'
# try:
# with open(file_name_1) as file:
# fb = file.readlines()
# except FileNotFoundError:
# print("sorry,can't find your file")
# else:
# for line in fb:
# print(line.rstrip())
# try:
# with open(file_name_2) as fil:
# fv = fil.readlines()
# except FileNotFoundError:
# print("sorry,can't find your file")
# else:
# for line in fv:
# print(line.rstrip())
# 练习 10-9:
# file_name_1 = 'D:/1/lib/cats.txt'
# file_name_2 = 'D:/whatever/lib/dogs.txt'
# try:
# with open(file_name_1) as file:
# fb = file.readlines()
# except FileNotFoundError:
# pass
# else:
# for line in fb:
# print(line.rstrip())
# try:
# with open(file_name_2) as fil:
# fv = fil.readlines()
# except FileNotFoundError:
# pass
# else:
# for line in fv:
# print(line.rstrip())
# 练习 10-10:、
# def count_word(file_name):
# try:
# with open(file_name,encoding = 'utf-8') as f:
# contents = f.read()
# except FileNotFoundError:
# print(f'sorry,the file {file_name} dose not exit.')
# else:
# words = contents.split('the ')
# num_words = len(words)
# print(f'the file {file_name} has about {num_words} "the" words.')
# file_name = 'D:/whatever/lib/The Great Gatsby.txt'
# count_word(file_name)
# 练习 10-11:
# import json
# favorite_number = input('please input your favorite number:')
# file_name = 'number.json'
# with open(file_name,'w') as f:
# json.dump(favorite_number,f)
# with open(file_name) as f:
# numbers = json.load(f)
# print(f'I khow your favorite number! It is {numbers}.')
# 练习 10-12:
# import json
# try:
# file_name = 'numbere.json'
# with open(file_name) as f:
# numbers = json.load(f)
# except:
# favorite_number = input('please input your favorite number:')
# print('Thanks,i will remember that!')
# with open(file_name,'w') as f:
# json.dump(favorite_number,f)
# else:
# print(f'I khow your favorite number! It is {numbers}.')
# 练习 10-13:
# import json
# def get_stored_username():
# """如果存储过用户名,则获取他"""
# filename = 'username.json'
# try:
# with open(filename) as f_obj:
# username = json.load(f_obj)
# except FileNotFoundError:
# return None
# else:
# return username
# def get_new_username():
# """提示用户输入用户名"""
# username = input("What is your name?\n")
# filename = 'username.json'
# with open(filename, 'w') as f_obj:
# json.dump(username, f_obj)
# return username
# def greet_user():
# """问候用户,并指出其名字"""
# username = get_stored_username()
# if username:
# name = input(f'Is your username {username}?(yes/no)\n')
# if name == 'yes':
# print(f'Welcome back! {username}')
# else:
# username = get_new_username()
# print(f'we will remeber you when you come back,{username}.')
# else:
# username = get_new_username()
# print(f'we will remeber you when you come back,{username}.')
# greet_user()