fp = open("123.txt", "r")
s = f.read()
# 不关闭的话,此文件一直占用系统资源
f.close()
# 可以写成:
with open("123.txt", "r") as fp:
s = fp.read()
#这样写,就不需要手动调用close(),当with语句执行完毕之后,这个文件就会自动被python关闭
fp = open("123.txt", "r")
s = f.read()
# 不关闭的话,此文件一直占用系统资源
f.close()
# 可以写成:
with open("123.txt", "r") as fp:
s = fp.read()
#这样写,就不需要手动调用close(),当with语句执行完毕之后,这个文件就会自动被python关闭