Lab 1 of 5
Β·
Guided
Read a Device List and Print with Line Numbers
Objective: Open a text file of hostnames and print each one with a formatted line number.
Instructions
First, create a file called devices.txt with exactly this content β one hostname per line:
sw-core-01
sw-access-01
sw-access-02
rtr-edge-01
fw-dmz-01
Then create lab1.py and write a script that reads the file and prints each hostname with a right-aligned two-digit line number.
Your output must match exactly:
1. sw-core-01
2. sw-access-01
3. sw-access-02
4. rtr-edge-01
5. fw-dmz-01
Run it with python3 lab1.py. Paste your output below and submit.
Starter Code
with open("devices.txt") as f:
for num, line in enumerate(f, start=1):
print(f"{num:2}. {line.strip()}")
π‘ Show Hint
Use enumerate(f, start=1) to get the line number alongside each line. Use {num:2} in the f-string to right-align the number in 2 characters.
Paste Your Output