Lab 4 of 5
Β·
Semi-Guided
Filter Devices by Site Using sys.argv
Objective: Use sys.argv to accept a site name argument and filter a device list at runtime.
Instructions
Create a file called lab4.py. The script takes one command-line argument β a site name β and prints only the devices at that site.
Use this device list:
devices = [
{"name": "sw-core-01", "site": "HQ"},
{"name": "sw-access-01","site": "HQ"},
{"name": "rtr-edge-01", "site": "HQ"},
{"name": "sw-branch-01","site": "Branch"},
{"name": "rtr-branch-01","site":"Branch"},
]
If no argument is provided, print Usage: python3 lab4.py <site> and exit with code 1.
Run it with python3 lab4.py HQ. Your output must match exactly:
Site: HQ β 3 devices
sw-core-01
sw-access-01
rtr-edge-01
Run it with python3 lab4.py Branch. Your output must match exactly:
Site: Branch β 2 devices
sw-branch-01
rtr-branch-01
Paste the output for python3 lab4.py HQ below and submit.
π‘ Show Hint
Check len(sys.argv) < 2 to detect missing argument. Use sys.exit(1) after printing usage. Filter with a list comprehension: [d for d in devices if d["site"] == site].
Paste Your Output