python with 语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20



# with 可以在该语句结束后自动关闭
# 且with可以带多个参数

with open("log","w") as fw ,
open("log1","w") as fw1:
fw.write("1_linen2_linen3_linen")
fw1.write("11_linen22_linen33_linen44_line")

with open("log","r") as fr,
open("log1", "r") as fr1:
print(fr.readline())
print(fr.readline())
print(fr.readline())
print("------------------")

for line in fr1:
print(line)