CodeGym /Courses /Python SELF EN /Working with Binary Files

Working with Binary Files

Python SELF EN
Level 21 , Lesson 4
Available

5.1 Binary Data

Working with binary files in Python is a bit different from working with text files because binary files have data in a format that's not meant for a human's eye. Instead of strings, binary files deal with bytes.

To work with binary files, you use the open() function with a mode that includes the letter 'b' (like 'rb' for reading, 'wb' for writing, and so on).

Examples of opening binary files:

  • Reading: 'rb'
  • Writing: 'wb'
  • Appending: 'ab'
  • Reading and Writing: 'r+b', 'w+b', 'a+b'

Example:


file = open('example.bin', 'rb')
content = file.read()
print(content)
file.close()

The variable 'content' will contain a byte array.

Binary data (bytes) is the lowest, most basic level of data representation. Any data can be read as binary.

This means that a text file can always be read as binary, but not every binary file can be interpreted as text.

5.2 Reading Binary Files

Reading the entire file content

The read() method reads the entire content of the file in bytes.

Example:


file = open('example.bin', 'rb')
content = file.read()
print(content)
file.close()

Reading a specific number of bytes

The read(n) method reads n bytes from the file.

Example:


file = open('example.bin', 'rb')
content = file.read(10)  # Reads the first 10 bytes
print(content)
file.close()

Reading by line

The readline() method reads one line from the file. In the case of binary files, a line ends with the newline character (\n).

Example:


file = open('example.bin', 'rb')
line = file.readline()
print(line)
file.close()

Reading all lines

The readlines() method reads all lines from the file and returns them as a list of bytes.

Example:


file = open('example.bin', 'rb')
lines = file.readlines()
for line in lines:
    print(line)
file.close()

Methods for working with strings might not work correctly if you read a file that contains non-text data: like an archive, image, or video.

5.3 Writing Binary Data

The write() method writes bytes to a file. The data for writing must be in bytes (bytes).

Reading and Writing Images

Reading an image from a file and writing it to another file.


# Reading an image
with open('input_image.jpg', 'rb') as infile:
    image_data = infile.read()
        
# Writing an image
with open('output_image.jpg', 'wb') as outfile:
    outfile.write(image_data)

You can also write text data:

Example:


data = b"Hello, World!"
lines = [b"First line.\n", b"Second line.\n", b"Third line.\n"]
        
file = open('example.bin', 'wb')
file.write(data)
file.writelines(lines)
file.close()
2
Task
Python SELF EN, level 21, lesson 4
Locked
Reading a binary file
Reading a binary file
2
Task
Python SELF EN, level 21, lesson 4
Locked
Write binary data
Write binary data
1
Survey/quiz
Working with Files, level 21, lesson 4
Unavailable
Working with Files
Working with Files
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION