Lab 1 of 5
Β·
Guided
Check If Files Exist Using the os Module
Objective: Import the os module and use os.path.exists() to check for specific files.
Instructions
First, create a directory and two files in your terminal:
mkdir lab6
touch lab6/network_devices.txt lab6/site_map.txt
Then create a file called lab1.py in your working directory and run it. It should check for three specific files inside lab6/ and print whether each one was found or not.
Your output must match this format exactly:
Found : network_devices.txt
Found : site_map.txt
Not found : lab_config.txt
Run it with python3 lab1.py. Paste your output below and submit.
Starter Code
import os
files = ["network_devices.txt", "site_map.txt", "lab_config.txt"]
for filename in files:
path = os.path.join("lab6", filename)
if os.path.exists(path):
print(f"Found : {filename}")
else:
print(f"Not found : {filename}")
π‘ Show Hint
Use os.path.join("lab6", filename) to build the path. os.path.exists() returns True or False.
Paste Your Output