Change the visualize.py program to receive the maximum number of bytes to print and how many bytes per line as command line parameters.
Answer:
##############################################################################
# Python From Scratch
# Author: Nilo Ney Coutinho Menezes
# Editora Novatec (c) 2010-2025 - LogiKraft 2025
# Site: https://pythonfromscratch.com
# ISBN: 978-85-7522-949-1 (Paperback), 978-85-7522-950-7 (hardcover), 978-85-7522-951-4 (ebook)
#
# File: chapter 09/exercise-09-41.py.py
##############################################################################
import sys
import itertools
def print_bytes(image, bytes_per_line=16):
for b in itertools.batched(image, bytes_per_line):
hex_view = " ".join([f"{v:02x}" for v in b])
tview = "".join([chr(v) if chr(v).isprintable() else "." for v in b])
print(f"{hex_view} {" " * 3 * (bytes_per_line - len(b))}{tview}")
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: python program.py file max_bytes bytes_per_line")
sys.exit(1)
file = sys.argv[1]
max_bytes = int(sys.argv[2])
bytes_per_line = int(sys.argv[3])
with open(file, "rb") as f:
image = f.read(max_bytes)
print_bytes(image, bytes_per_line)