Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách mở file nhị phân read/write mode Python?


Mở file nhị phân binary read/write, chỉ định 'w+b' (w=write, b=binary).

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

Mở my_file.mp3 binary read/write, lưu content vào biến, ghi đè "Hello" dạng binary. Cũng dùng r+b không truncate file.