
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to open a binary file in read and write mode with Python?
To open binary files in binary read/write mode, specify 'w+b' as the mode(w=write, b=binary). For example,
f = open('my_file.mp3', 'w+b') file_content = f.read() f.write(b'Hello') f.close()
Above code opens my_file.mp3 in binary read/write mode, stores the file content in file_content variable and rewrites the file to contain "Hello" in binary. You can also use r+mode as it doesn't truncate the file.
- Related Articles
- How to open a file in read and write mode with Python?
- Python Program to open a file in read-write mode with truncating file
- How to open a file in binary mode with Python?
- Golang Program to open a file in read-write mode with truncating file
- How to open a binary file in append mode with Python?
- Python Program to open a file in the read-write mode without truncating the file
- Golang Program to open a file in the read-write mode without truncating the file
- How to open a file in append mode with Python?
- Golang Program to open a file in read-only mode
- How to open a file just to read in python?
- Golang Program to open a file in write-only mode
- How to open a file to write in Python?
- How to set read and write position in a file in Python?
- Golang Program to open a file in append mode
- How to write binary data to a file using Python?

Advertisements