How to open a binary file in append mode with Python?


"Binary" files are any files where the format isn't made up of readable characters. Binary files can range from image files like GIFs, audio files like MP3s or binary document formats like Word or PDF. To open files in binary append mode, when specifying a mode, add 'ab' to it.

For example

 f = open('my_file.mp3', 'ab')
file_content = f.read()
f.close()

Above code opens my_file.mp3 in binary append mode and stores the file content in file_content variable.

Updated on: 12-Dec-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements