122. Using os for OS-level Operations
import os
# Check if a file exists
file_path = 'test_file.txt'
if os.path.exists(file_path):
print(f"{file_path} exists.")
else:
print(f"{file_path} does not exist.")import os
# Create a directory
directory_path = 'my_folder'
if not os.path.exists(directory_path):
os.mkdir(directory_path)
print(f"Directory '{directory_path}' created.")
else:
print(f"Directory '{directory_path}' already exists.")Last updated