python——文件——with

版权声明:©2004 Microsoft Corporation. All rights reserved. https://blog.csdn.net/qq_42036824/article/details/86601314

文件——with

  • 上下文管理器:打开文件,执行完with语句内容之后,自动关闭文件
    对象
#同时打开两个文件对象
with open('/tmp/passwd') as f1,open('/tmp/passwdbackup','w+') as f2:
    #将第一个文件的内容写入到第二个文件中
    f2.write(f1.read())
    #移动指针到文件最开始
    f2.seek(0)
    #读取文件内容
    print(f2.read())

猜你喜欢

转载自blog.csdn.net/qq_42036824/article/details/86601314