Writes given to_write string (should only consist of 0's and 1's) as bytes in the file
(file_path: str, to_write: str)
| 55 | |
| 56 | |
| 57 | def write_file_binary(file_path: str, to_write: str) -> None: |
| 58 | """ |
| 59 | Writes given to_write string (should only consist of 0's and 1's) as bytes in the |
| 60 | file |
| 61 | """ |
| 62 | byte_length = 8 |
| 63 | try: |
| 64 | with open(file_path, "wb") as opened_file: |
| 65 | result_byte_array = [ |
| 66 | to_write[i : i + byte_length] |
| 67 | for i in range(0, len(to_write), byte_length) |
| 68 | ] |
| 69 | |
| 70 | if len(result_byte_array[-1]) % byte_length == 0: |
| 71 | result_byte_array.append("10000000") |
| 72 | else: |
| 73 | result_byte_array[-1] += "1" + "0" * ( |
| 74 | byte_length - len(result_byte_array[-1]) - 1 |
| 75 | ) |
| 76 | |
| 77 | for elem in result_byte_array[:-1]: |
| 78 | opened_file.write(int(elem, 2).to_bytes(1, byteorder="big")) |
| 79 | except OSError: |
| 80 | print("File not accessible") |
| 81 | sys.exit() |
| 82 | |
| 83 | |
| 84 | def remove_prefix(data_bits: str) -> str: |