Extract Text Information from a Binary File - Python Programming Exercise

In this exercise, you will develop a Python program to extract only the alphabetic characters contained in a binary file and dump them into a separate file. This exercise is perfect for practicing file handling, byte manipulation, and string processing in Python. By implementing this program, you will gain hands-on experience in handling file operations, byte manipulation, and string processing in Python. This exercise not only reinforces your understanding of file handling but also helps you develop efficient coding practices for managing user interactions.

 Category

Managing Files

 Exercise

Extract Text Information From A Binary File

 Objective

Develop Python Program to extract only the alphabetic characters contained in a binary file and dump them into a separate file. The extracted characters should be those whose ASCII code is between 32 and 127, or equal to 10 (newline) or 13 (carriage return).

 Example Python Exercise

 Copy Python Code
# Python program to extract alphabetic characters from a binary file and dump them into a separate file

def extract_alphabetic_characters(input_file_path, output_file_path):
    try:
        # Open the input binary file in read mode
        with open(input_file_path, 'rb') as input_file:
            # Open the output text file in write mode
            with open(output_file_path, 'w') as output_file:
                # Read the binary file byte by byte
                byte = input_file.read(1)
                
                # Process each byte
                while byte:
                    # Convert byte to integer (ASCII value)
                    ascii_value = ord(byte)
                    
                    # Check if the byte is a printable ASCII character, newline, or carriage return
                    if (32 <= ascii_value <= 126) or ascii_value in [10, 13]:
                        # Write the valid character to the output file
                        output_file.write(byte.decode('ascii', errors='ignore'))
                    
                    # Read the next byte
                    byte = input_file.read(1)
                
                print(f"Alphabetic characters have been extracted and saved to '{output_file_path}'.")
    
    except FileNotFoundError:
        print(f"Error: The file '{input_file_path}' was not found.")
    except Exception as e:
        print(f"Error: {str(e)}")

# Example usage
extract_alphabetic_characters('input_file.bin', 'output_file.txt')

 Output

Example Input (input_file.bin):

Hello, World! This is a test.
12345!@#$%^&*()_+[]{}|;:,.<>?/
Some binary content: \x01\x02\x03

Newline character follows:

This is another line.
Example Output (output_file.txt):

Hello, World! This is a test.
12345!@#$%^&*()_+[]{}|;:,.<>?/

Some binary content: 

Newline character follows:

This is another line.

Error Scenarios:

File Not Found:

Error: The file 'input_file.bin' was not found.

General Errors:

Error: Error message

 Share this Python Exercise

 More Python Programming Exercises of Managing Files

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Dump

    In this exercise, you will develop a Python program to create a "dump" utility: a hex viewer that displays the contents of a file, with 16 bytes in each row and 24 ro...

  •  Text Filter

    In this exercise, you will develop a Python program to create a utility that censors text files. This exercise is perfect for practicing file handling, string ...

  •  SQL to Plain Text

    In this exercise, you will develop a Python program to parse SQL INSERT commands and extract their data into separate lines of text. This exercise is perfect f...

  •  PGM Image Viewer

    In this exercise, you will develop a Python program to create a utility that reads and displays images in the PGM format, which is a version of the NetPBM image forma...

  •  Console BMP Viewer V2

    In this exercise, you will develop a Python program to create a utility that displays a 72x24 BMP file on the console. This exercise is perfect for practicing ...

  •  Saving Data to a Text File

    In this exercise, you will develop a Python program to collect multiple sentences from the user (continuing until the user presses Enter without typing anything) and ...