How to Check if a Certain Path Exists
The first thing we will go over is how to check if a certain path exists in an operating system.
The first thing we must do is import the os module.
After this, we use the statement, os.path.exists(path), to determine whether the path exists.
If you were to run this code you would get the following output shown below:
How to Check if an Object is a Directory in Python
To check whether an object in a path specified is a directory (as opposed to a file),
we use the statement, os.path.isdir(directory_name),
If the object is a directory, True will be returned.
If the object is not a directory, False will be returned.
The code, below, checks to see whether, C:\\Users, is a directory:
Since it is a directory, True is returned.
How to Check if an Object is a File in Python
To check whether an object in a path specified is a file (as opposed to a directory),
we use the statement, os.path.isfile(file_name),
If the object is a file, True will be returned.
If the object is not a file, False will be returned.
The code, below, checks to see whether, C:\\Users, is a file:
Since it is not a file, False is returned