With context manager in Python

with read file content

#  
一种
# 1. Open the file one_file = open ( " test1.txt " , encoding = " utf-8 " )

# 2 write operation 
# using the read method, all the contents of the file will be read out, in order to render the string type 
Content = one_file.read ()
 Print (Content)       # print content read

# 3. Close the file 
one_file.close ()

# The second: with automatically closes the file 
with Open ( " test1.txt " , encoding = " UTF-8 " ) AS b:
    content = b.read()
    print(content)

 

with can copy files, separated by commas

  

with open ( " keyou_2019-07-03_21-54-52.png " , mode = " rb " ) as one_file, open ( " keyou.png " , mode = " wb " ) as two_file:
     # 2. Read and write operation 
    content one_file.read = ()   # reading the first binary image data 
    two_file.write (Content)   # binary data read, written to the second file

 

******* Please respect the original, if you want to reprint, please indicate the source: Reprinted from: https://www.cnblogs.com/shouhu/ , thank you! ! ******* 

Guess you like

Origin www.cnblogs.com/shouhu/p/12741242.html