# -*- coding: utf-8 -*-# 10-1python学习笔记# 读取整个文件withopen('G:\python\Pwork\python编程从入门到实战\learning_python.txt', 'r') asfile_object:
contents = file_object.read()
contents = contents.replace('Python', 'C')
print(contents + '\n')
# 遍历文件对象withopen('G:\python\Pwork\python编程从入门到实战\learning_python.txt') asfile_object:
forline infile_object:
line = line.replace('Python', 'C')
print(line + '\n')
# 将各行存储在一个列表中withopen('G:\python\Pwork\python编程从入门到实战\learning_python.txt') asfile_object:
lines = file_object.readlines()
forline inlines:
line = line.replace('Python', 'C')
print(line + '\n')
# 10-3访客withopen('guest.txt', 'w') asfile_object:
name = input('Please input your name:')
file_object.write(name)
# 10-4访客名单withopen('guest_book.txt', 'a') asfile_object:
print('If you want to quit , please entert "q".')
whileTrue:
name = input('Please enter your name:')
ifname == 'q':
breakprint('Hello , '+ name + ' . Nice to meet you!')
file_object.write(name + '\n')
# 10-5关于编程的调查withopen('research.txt', 'a') asfile_object:
print('If you want to quit , please entert "q".')
whileTrue:
name = input('Please enter your name:')
ifname == 'q':
breakreason = input('Why do you like program?')
ifreason == 'q':
breakfile_object.write('Name:'+ name + '\n')
file_object.write('Reason:'+ reason + '\n\n')
# 10-6加法运算first_number = input('\nFirst Number:')
second_number = input('\nSecond Number:')
try:
answer = int(first_number) + int(second_number)
exceptValueError:
print('You can\'t add two string.')
else:
print(answer)
# 10-7加法计算器print('Give me two number , and I\'ll add them.')
print('If you want to quit , please enter "q".')
whileTrue:
first_number = input('\nFirst Number:')
iffirst_number == 'q':
breaksecond_number = input('\nSecond Number:')
ifsecond_number == 'q':
breaktry:
answer = int(first_number) + int(second_number)
exceptValueError:
passelse:
print(answer)
# 10-8猫和狗withopen('cats.txt', 'a') asfile_object:
print('If you want to quit , please entert "q".')
whileTrue:
cats_name = input('Please enter your cat\'s name:')
ifcats_name == 'q':
breakfile_object.write(cats_name + '\n')
withopen('dogs.txt', 'a') asfile_object:
print('If you want to quit , please enter "q".')
whileTrue:
dogs_name = input('Please enter your dog\'s name:')
ifdogs_name == 'q':
breakfile_object.write(dogs_name + '\n')
defcount_name(filename):
"""计算一个文件中包含多少个名字"""try:
withopen(filename) asfile_object:
contents = file_object.read()
exceptFileNotFoundError:
print('Sorry , the file '+ filename + 'does not exist.')
else:
names = contents.split()
count_name = len(names)
print('The file '+ filename + ' has about '+ str(count_name) + ' names.')
filenames = ['cats.txt', 'dogs.txt']
forfilename infilenames:
count_name(filename)
#10-11喜欢的数字importjson
filename = 'favorite_number.json'withopen(filename,'w') asf_obj:
'''将数字存储到文件'''favorite_number = input('What\'s your favorite number? ')
json.dump(favorite_number,f_obj)
withopen(filename) asf_obj:
'''读取文件中的内容'''favorite_number = json.load(f_obj)
message = 'I know your favorite number . It\'s 'print(message + favorite_number + '.')
#10-12记住喜欢的数字importjson
filename = 'favorite_number.json'try:
withopen(filename) asf_obj:
favorite_number = json.load(f_obj)
exceptFileNotFoundError:
withopen(filename,'w') asf_obj:
favorite_number = input('What\'s your favorite number ?\n')
json.dump(favorite_number,f_obj)
else:
message = 'I know your favorite number . It\'s 'print(message + favorite_number + '.\n')
#10-13验证用户importjson
defget_stored_username():
''''如果存储了用户名,就获取'''filename = 'username.json'try:
withopen(filename) asf_obj:
username = json.load(f_obj)
exceptFileNotFoundError:
returnNoneelse:
returnusername
defstored_new_username():
''''提示用户输入用户名,并存储到文件中'''filename = 'username.json'username = input('What\'s your name?\n')
withopen(filename,'w') asf_obj:
json.dump(username,f_obj)
returnusername
defgreet_user():
''''问候用户,并指出其名字'''username = get_stored_username()
ifusername:
current_username = input('Is '+username.title() + ' your name?\n')
print('(Enter "Y/y" for yes.)\n')
ifcurrent_username.lower() == 'y':
print('Welcome back , '+ username.title() + '!\n')
else:
username = stored_new_username()
print('We\'ll remenber you when you come back , '+ username.title() + '!\n')
greet_user()