文件

1
2
3
with open('123.txt','r') as file_object:
for line in file_object:
print(line.rstrip())
  • open( ) close( ) 打开和关闭文件可以通过这两个控制,也可以在前面加制导with让系统控制文件关闭
  • mode有
    • w 清空写入
    • r 只读
    • a 追加写入
    • r+ 读写模式

方法鉴赏

异常

1
2
3
4
5
6
try:
print(5/0)
except ZeroDivisionError:
print("You can't divide by zero!")
else:
print(answer)
  • pass 静默失败

存储数据

1
2
3
4
import json

json.dump(var,filen_object)
json.load(file_object)

测试 unittest

待补···