Skip to content

Commit ef2bb70

Browse files
committed
[C]增加了第10章 文件和异常的相关操作
Signed-off-by: VincentJ <im.vincentj@gmail.com>
1 parent a1cb1da commit ef2bb70

10 files changed

+114
-0
lines changed

chapter9/alice.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 处理FileNotFoundError 异常
2+
filename = 'alice.txt'
3+
try:
4+
with open(filename) as f_obj:
5+
contents = f_obj.read()
6+
except FileNotFoundError:
7+
msg = "Sorry, the file " + filename + " does not exist."
8+
print(msg)
9+
10+
# 分析文本
11+
title = "Alice in Wonderland"
12+
print(title.split())

chapter9/alice.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
In the first half of the book, you’ll learn about basic programming concepts, such as lists, dictionaries, classes, and loops, and practice writing clean and readable code with exercises for each topic. You’ll also learn how to make your programs interactive and how to test your code safely before adding it to a project. In the second half of the book, you’ll put your new knowledge into practice with three substantial projects: a Space Invaders–inspired arcade game, data visualizations with Python’s super-handy libraries, and a simple web app you can deploy online.

chapter9/exception.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 异常处理
2+
3+
try:
4+
print(5 / 0)
5+
except ZeroDivisionError:
6+
print("You can't divide by zero!")
7+
8+
# 依赖于try 代码块成功执行的代码都应放到else代码块中
9+
print("Give me two numbers, and I'll divide them.")
10+
print("Enter 'q' to quit.")
11+
while True:
12+
first_number = input("\nFirst number: ")
13+
if first_number == 'q':
14+
break
15+
second_number = input("Second number: ")
16+
try:
17+
answer = int(first_number) / int(second_number)
18+
except ZeroDivisionError:
19+
pass
20+
print("You can't divide by 0!")
21+
else:
22+
print(answer)

chapter9/file_reader.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 读文件
2+
with open('pi_digits.txt') as file_object:
3+
contents = file_object.read()
4+
# print(contents)
5+
# print(contents.rstrip())
6+
7+
# 分行打印--这里可以看到空行
8+
# for line in file_object:
9+
# print(line)
10+
# 去空行
11+
# print(line.rstrip())
12+
13+
# 一次读出所有行
14+
# lines = file_object.readlines()
15+
# for line in lines:
16+
# print(line.rstrip())
17+
18+
# 其他位置
19+
with open('../pi.txt') as file_obj:
20+
contents2 = file_obj.read()
21+
print(">>>" + contents2)
22+
# 去除开头的空格
23+
print('>>>' + contents2.strip())
24+
25+
print('1415' in contents2)

chapter9/number_reader.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 读json
2+
import json
3+
4+
file_name = "number.txt"
5+
with open(file_name) as file_object:
6+
numbers = json.load(file_object)
7+
print(numbers)

chapter9/number_writer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 存储数据--写json
2+
3+
import json
4+
5+
numbers = [2, 3, 5, 7, 11, 13]
6+
file_name = "number.txt"
7+
with open(file_name, 'w') as file_object:
8+
json.dump(numbers, file_object)

chapter9/pi_digits.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
3.1415926535
2+
8979323846
3+
2643383279

chapter9/word_count.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def count_words(filename):
2+
"""计算一个文件大致包含多少个单词"""
3+
try:
4+
with open(filename) as f_obj:
5+
contents = f_obj.read()
6+
except FileNotFoundError:
7+
# 当然也可以使用pass忽略该异常提醒
8+
# pass
9+
msg = "Sorry, the file " + filename + " does not exist."
10+
print(msg)
11+
else:
12+
# 计算文件大致包含多少个单词
13+
words = contents.split()
14+
num_words = len(words)
15+
print("The file " + filename + " has about " + str(num_words) + " words.")
16+
17+
18+
filename = 'alice.txt'
19+
count_words(filename)

chapter9/write_message.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 写入文件
2+
file_name = 'programming.txt'
3+
# 默认不传'w'是指只打开文件'r'
4+
# 读取模式('r')、写入模式('w')、附加模式('a')或让你能够读取和写入文件的模式('r+')
5+
with open(file_name, 'w') as file_object:
6+
# 写单行
7+
file_object.write("I like Python.")
8+
# 在写一行-- 发现挤在一起了
9+
file_object.write("I like Kotlin.\n")
10+
# 加换行符即可
11+
file_object.write("I like Julia.\n")
12+
13+
# 额外添加信息到文本末尾
14+
with open(file_name, 'a') as file_object:
15+
file_object.write("I also love finding meaning in large datasets.\n")
16+
file_object.write("I love creating apps that can run in a browser.\n")

pi.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.1415926535 8979323846 2643383279

0 commit comments

Comments
 (0)