上下文管理器

文件上下文管理器自动关闭文件

"""
with open() as 名字
student.txt
hello world
word ppt excel

"""
## 读取student的内容复制到b中
with open("student.txt","r") as f,open("b.txt","w+") as f2:
   f2.write(f.read())
   f2.seek(0,0)##因为读取后指针在最后所以需要将指针移动到开头
   print(f2.read())

 

上一篇:luogu P6229 [BalticOI 2019 Day2]项链


下一篇:python 修改文件