天天看点

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

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

"""
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())