Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีเปิด Binary File ใน Read/Write Mode ด้วย Python

ระบุ mode 'w+b' สำหรับ binary read/write (w=write, b=binary):

f = open('my_file.mp3', 'w+b')
file_content = f.read()
f.write(b'Hello')
f.close()

จะเปิด binary read/write เก็บเนื้อหาใน file_content และเขียน "Hello" แบบ binary ทับไฟล์ สามารถใช้ r+b แทน (ไม่ truncate ไฟล์)