python in reading and writing binary files

reference:

 

 

 

 

  1.  
     
  2.  
    # f = open("test4.txt", 'w')
  3.  
    #
  4.  
    # f.write(b'hello world') # TypeError: write() argument must be str, not bytes
  5.  
    #
  6.  
    # f.close()
  7.  
     
  8.  
    Open = f ( "test4.txt" , 'wb' ) # write binary mode
  9.  
     
  10.  
    f.write ( b'hello world ' ) # binary write
  11.  
     
  12.  
    f.close () # close the file
  13.  
     
  14.  
    Open = F ( "the test4.txt files" , 'RB' ) # binary read
  15.  
     
  16.  
    Print (reached, f.read ()) # b'hello World 'print data read out
  17.  
     
  18.  
    f.close () # close the file
  19.  
     

 

Guess you like

Origin www.cnblogs.com/kuangke/p/12390615.html