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
Published on 27-Dec-2017 06:45:56
Advertisements