I wrote this to check files for successful compression with LZ4.
Any advice is welcome.
Particularly regarding raising exceptions and error handling.
import sys
def check_lz4_header(file_path):
with open(file_path, 'rb') as file:
header = file.read(4) # Read the first 4 bytes (typical size of LZ4 header)
if header != b'\x04\x22\x4d\x18': # Check for LZ4 magic number (optional)
raise ValueError("Invalid header: This does not appear to be an LZ4 compressed file.")
else:
print("This appears to be an LZ4 compressed file.")
# If the script is run as a standalone script, execute the check_lz4_header function
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python lz4_header_checker.py <file_path>", file=sys.stderr)
sys.exit(1)
file_path = sys.argv[1]
try:
check_lz4_header(file_path)
except ValueError as e:
print(e, file=sys.stderr)
sys.exit(1)