The path.exists method of the os module
The path.exists method checks whether the path specified in the first parameter of the method exists. The method returns a boolean value, if the path exists - then True, otherwise - False.
Syntax
import os
print(os.path.exists(path))
Example
Let's check if the folder dir exists:
import os
print(os.path.exists('dir'))
The result of the executed code:
True
Example
Now let's check for a path that doesn't exist:
import os
print(os.path.exists('dir1'))
The result of the executed code:
False
See also
-
method
path.joinof moduleos,
which unites the paths -
method
path.isdirof moduleos,
which checks the existence of a folder -
method
path.isfileof moduleos,
which checks the existence of a file -
method
path.getsizeof moduleos,
which returns the size of the path in bytes -
method
statof moduleos,
which returns the status of the path