The difference between open () and with open () of

open

  1, open the file

    file = open ( "filename", "read-write mode")

  2, manipulate files

    ***

  3, close the file

    file.close()

  Precautions :

    Using the open method, the file must be closed after the operation is completed, or long-term stay connected to the file, causing memory overflow occurred.

with open

  1, open the file

    with open ( "filename", "read-write") as file: 

  2, manipulate files

    ***

  3, close the file (automatically turn off)

  

Guess you like

Origin www.cnblogs.com/yangzhe0617/p/11038723.html