How do I check if directory exists in Python?
You can use the os
module to check if a directory exists. Here is an example of how you can do it:
import os
# Check if directory exists
if os.path.isdir(directory):
# Do something
pass
else:
# Do something else
pass
Watch a video course
Python - The Practical Guide
Alternatively, you can use the os.path.exists()
function to check if the directory exists. This function returns True
if the directory exists, and False
if it does not.
Here is an example of how you can use os.path.exists()
to check if a directory exists:
import os
# Check if directory exists
if os.path.exists(directory):
# Do something
pass
else:
# Do something else
pass