How to open a file in binary mode with Python?

PythonServer Side ProgrammingProgramming

"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 = f.read()
f.close()

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

raja
Published on 21-Dec-2017 17:57:14
Advertisements