Save a 1D NumPy array to a binary file. Args: filename (str): The name of the file to save the array. array (np.ndarray): The 1D array to save.
(filename, array)
| 54 | return None, None |
| 55 | |
| 56 | def save_array_to_bin(filename, array): |
| 57 | """ |
| 58 | Save a 1D NumPy array to a binary file. |
| 59 | |
| 60 | Args: |
| 61 | filename (str): The name of the file to save the array. |
| 62 | array (np.ndarray): The 1D array to save. |
| 63 | """ |
| 64 | try: |
| 65 | with open(filename, "wb") as file: |
| 66 | # Write the length of the array as a 4-byte integer |
| 67 | length = array.size |
| 68 | file.write(length.to_bytes(4, byteorder='little')) |
| 69 | |
| 70 | # Write the array data |
| 71 | array.tofile(file) |
| 72 | |
| 73 | print(f"Array saved to {filename}. Length: {length}") |
| 74 | except IOError: |
| 75 | print("Error: Cannot write to file.") |
| 76 | |
| 77 | def load_array_from_bin(filename): |
| 78 | """ |