#10-1
#1
file_name='E:\\Python\\file\\learning_python.txt'
with open(file_name) as file_object:
contents=file_object.read()
print(contents)
#2
file_name='E:\\Python\\file\\learning_python.txt'
with open(file_name) as file_object:
for line in file_object:
print(line)
#3
file_name='E:\\Python\\file\\learning_python.txt'
with open(file_name) as file_object:
lines=file_object.readlines()
text=''
for line in lines:
text+=line.strip()
print(text)
print(len(text))
#10-2
file_name='E:\\Python\\file\\learning_python.txt'
with open(file_name) as file_object:
con=file_object.read()
cons=con.replace('python','java')
print(cons)
10-3
file='guest.txt'
with open(file,'w') as file_object:
msg=input("please input your name:")
file_object.write(msg)
print(msg)
10-4
file='guest.txt'
with open(file,'a') as file_object:
print("when you input q ,it's will stop.")
q=True
while q:
msg=input("please input your name:")
if msg!='q':
print("Hello,"+msg)
file_object.write(msg+"\n")
else:
q=False
file='reason.txt'
with open(file,'w') as file_object:
ask='Y'
while ask=='Y':
msg=input("Why do you like programming?")
file_object.write(msg+"\n")
ask=input("Do you want to ask next one?(Y or N)")
10-6
print("please input two numbers")
print("Enter 'q' will quit.")
while True:
number_1=input("First_number:")
if number_1=='q':
break
number_2=input("second_number:")
if number_2=='q':
break
try:
n1=int(number_1)
n2=int(number_2)
except ValueError:
print("the contents you input is not number!")
else:
n=n1+n2
print(n)
10-7
print("please input two numbers")
print("Enter 'q' will quit.")
while True:
number_1=input("First_number:")
if number_1=='q':
break
number_2=input("second_number:")
if number_2=='q':
break
try:
n1=int(number_1)
n2=int(number_2)
except ValueError:
pass
else:
n=n1+n2
print(n)
file_name1='cats.txt'
file_name2='dogs.txt'
try:
with open(file_name1) as file_object:
f1=file_object.read()
print(f1)
with open(file_name2) as file_object:
f2=file_object.read()
print(f2)
except FileNotFoundError:
print("Can't find the file.")
file_name1='cat.txt'
file_name2='dog.txt'
try:
with open(file_name1) as file_object:
f1=file_object.read()
print(f1)
with open(file_name2) as file_object:
f2=file_object.read()
print(f2)
except FileNotFoundError:
pass
def count_specific_word(filename,word):
try:
with open(file_name) as file_object:
contents=file_object.read()
except FileNotFoundError:
msg="Sorry, the file "+file_name+" does not exist."
print(msg)
else:
num=contents.split().count(word)
print("the number of "+"'"+word+"'"+" is "+str(num)+"\n")
file_names=['alice.txt']
for file_name in file_names:
count_specific_word(file_name,'the')
10-11
import json
file_name='numbers.json'
with open(file_name,'w') as file_object:
msg=input("what number do you like?")
json.dump(msg,file_object)
file_name='numbers.json'
with open(file_name) as file_object:
num=json.load(file_object)
print("I know your favorite number!It's "+num+".")
10-12
import json
def store_number():
file_name='numbers.json'
with open(file_name,'w') as file_object:
msg=input("what number do you like?")
json.dump(msg,file_object)
# return number
def get_favorite_number():
file_name='numbers.json'
with open(file_name) as file_object:
num=json.load(file_object)
return num
def favorite_number():
number=get_favorite_number()
if number:
# num=get_favorite_number()
print("I know your favorite number!It's "+number+".")
else:
store_number()
favorite_number()
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?")
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:
if username==get_new_username():
print("Welcome back, "+username+"!")
else:
get_new_username()
else:
username=get_new_username()
print("We'll remember you when you come back, "+username+"!")
greet_user()