Lab 1 of 5
Β·
Guided
List Config Files Using os and glob
Objective: Use os.makedirs() to create a directory structure, then use glob to list files by extension.
Instructions
Run the following commands in your terminal to create a test directory with files:
mkdir -p lab10
touch lab10/sw-core-01.cfg lab10/rtr-edge-01.cfg lab10/fw-dmz-01.cfg
touch lab10/devices.json lab10/notes.txt
Then create lab1.py:
- Use
glob.glob()to list all.cfgfiles inlab10/ - Print the count and each filename (basename only, not the full path)
- Use
glob.glob()to list all.jsonfiles inlab10/ - Print the count and each filename
Your output must match exactly:
CFG files: 3
fw-dmz-01.cfg
rtr-edge-01.cfg
sw-core-01.cfg
JSON files: 1
devices.json
Run it with python3 lab1.py. Paste your output below and submit.
Starter Code
import glob
import os
cfg_files = sorted(glob.glob("lab10/*.cfg"))
print(f"CFG files: {len(cfg_files)}")
for f in cfg_files:
print(os.path.basename(f))
json_files = sorted(glob.glob("lab10/*.json"))
print(f"JSON files: {len(json_files)}")
for f in json_files:
print(os.path.basename(f))
π‘ Show Hint
Use sorted() on the glob result so files appear in alphabetical order. os.path.basename() strips the directory prefix and returns just the filename.
Paste Your Output