python中讀寫二進制文件


參考:

 

 

 

 

  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.  
    f = open( "test4.txt", 'wb') # 二進制寫模式
  9.  
     
  10.  
    f.write( b'hello world') # 二進制寫
  11.  
     
  12.  
    f.close() # 關閉文件
  13.  
     
  14.  
    f = open( "test4.txt", 'rb') # 二進制讀
  15.  
     
  16.  
    print(f.read()) # b'hello world' 打印讀出來的數據
  17.  
     
  18.  
    f.close() # 關閉文件
  19.  
     

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM