File "nhị phân" là file format không gồm ký tự đọc được. Bao gồm ảnh JPEG/GIF, audio MP3, định dạng tài liệu nhị phân Word/PDF. Python mở file text mode mặc định. Để mở binary mode, thêm 'b' vào mode.
Ví dụ
f = open('my_file', 'w+b')
byte_arr = [120, 3, 255, 0, 100]
binary_format = bytearray(byte_arr)
f.write(binary_format)
f.close()
Mở file binary write mode, ghi nội dung mảng byte_arr dưới dạng byte vào file nhị phân my_file.