本章知识点:
1.如何使用文件。
2.如何一次性读取整个文件。
3.如何以每次一行的方式读取文件的内容。
4.如何写入文件。
5.如何将文本附加到文件末尾。
6.如何处理程序可能引发的异常。
7.如何存储python数据结构,以保存用户提供的信息,避免用户每次运行程序时都需要重新提供。
习题:
10-1 Python学习笔记
file_name = "learning_python.txt"
with open(file_name) as f:
tmp = f.read()
print(tmp.rstrip())
with open(file_name) as f:
for line in f:
print(line.rstrip())
with open(file_name) as f:
lines = f.readlines()
for line in lines:
print(line.rstrip())
输出:
In python you can program more concisely.
In python you can solve many problems.
In python you can use data structure.
In python you can program more concisely.
In python you can solve many problems.
In python you can use data structure.
In python you can program more concisely.
In python you can solve many problems.
In python you can use data structure.
10-2 C语言学习笔记
file_name = "learning_python.txt"
with open(file_name) as f:
lines = f.readlines()
for line in lines:
line.replace('python','c')
print(line.replace('python','c').rstrip())
输出:
In c you can program more concisely.
In c you can solve many problems.
In c you can use data structure.
10-3访客
name = input("Please input your name.")
file_name = "name.txt"
with open(file_name,'w') as f:
f.write(name)
输出:
Please input your name.Huang Heng
文件输出:
Huang Heng
10-4访客名单
file_name = "name1.txt"
while (1) :
name = input("please input your name,input 'q' to quit.")
if name == "q":
break
str1 = "Nice to meet you, " + name
print(str1)
str1 = str1+"\n"
with open(file_name,'a') as f:
f.write(str1)
输出:
please input your name,input 'q' to quit.Lin Feng
Nice to meet you, Lin Feng
please input your name,input 'q' to quit.Zhang Hong
Nice to meet you, Zhang Hong
please input your name,input 'q' to quit.Liu Kui
Nice to meet you, Liu Kui
文件输出:
Nice to meet you, Lin Feng
Nice to meet you, Zhang Hong
Nice to meet you, Liu Kui
10-5关于编程的调查
file_name = "reason.txt"
while (1) :
reason = input("Why do you like programming?")
if reason == "q":
break
reason = reason + "\n"
with open(file_name,'a') as f:
f.write(reason)
输出:
Why do you like programming?Interesting
Why do you like programming?Funny
Why do you like programming?Great
文件输出:
Interesting
Funny
Great
10-6加法运算
try:
num1 = int(input("first number"))
num2 = int(input("second number"))
num3 = num1+num2
except ValueError:
print("Invalid number.")
else:
print(num3)
输出:
first number20
second number30
50
first numberlasdkja
Invalid number.
10-7加法计算器
while(1):
try:
num1 = int(input("first number: "))
num2 = int(input("second number: "))
if (num1 == 0):
break
num3 = num1 + num2
except ValueError:
print("Invalid number.")
else:
print(num3)
输出:
first number: 20
second number: 30
50
first number: akdjal
Invalid number.
first number:
10-8猫和狗
try:
file1 = "cat.txt"
file2 = "dog.txt"
with open(file1) as f1:
content1s = f1.read()
with open(file2) as f2:
content2s = f2.read()
except FileNotFoundError:
print("Can not find the file.")
else:
print(content1s.rstrip())
print(content2s.rstrip())
输出:
Kelly
Jam
Tony
Mike
Sara
Bill
Can not find the file.
10-9沉默的猫和狗
try:
file1 = "cat.txt"
file2 = "dog.txt"
with open(file1) as f1:
content1s = f1.read()
with open(file2) as f2:
content2s = f2.read()
except FileNotFoundError:
pass
else:
print(content1s.rstrip())
print(content2s.rstrip())
10-11喜欢的数字
import json
file_name = "favourite.json"
number = input("Input your favourite number.")
with open(file_name,'w') as f:
json.dump(number,f)
with open(file_name) as f:
number1 = json.load(f)
print("I know your favourite number!It's "+ number1)
输出:
Input your favourite number.30
I know your favourite number!It's 30
10-12
记住喜欢的数字
import json
file_name = "favourite.json"
try:
with open(file_name) as f:
number = json.load(f)
except FileNotFoundError:
number = input("Input your favourite number.")
with open(file_name,'w') as f:
json.dump(number,f)
print('We will remember you when you come back, ' + number +"!")
else:
print("Welcome back, " + number + "!")
输出:
Input your favourite number.40
We will remember you when you come back, 40!
Welcome back, 40!