How to open a file in binary 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 JPEGs or GIFs, audio files like MP3s or binary document formats like Word or PDF. In Python, files are opened in text mode by default. To open files in binary mode, when specifying a mode, add 'b' to it.

For example

f = open('my_file.mp3', 'rb')
file_content =

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

Updated on: 12-Dec-2019

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started