with ... as 读写文件

it2023-05-09  75

python使用with as 读写文件

Python引入了with语句来自动帮我们调用close()方法,同时也解决了异常问题

with open('test.txt','r') as f : print(f.read())

等价于

try: f =open('test1.txt','r') print(f.read()) finally: f.close()
最新回复(0)