天天看點

上下文管理器檔案上下文管理器自動關閉檔案

檔案上下文管理器自動關閉檔案

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