How to find if a directory exists in Python?

PythonServer Side ProgrammingProgramming

Using the os module's os.path.exists(directory), you can check if a directory exists or not.

example

import os
if not os.path.exists('my_folder'):
    print("Given path doesn't exist")
else:
    print("Given path exists")

If you run this and folder is already there, you'll get the message −

"Given path exists"
raja
Updated on 18-Feb-2020 05:25:51

Advertisements