Search code examples
pythonpython-3.xvalidationinputpython-os

Python: Check if a directory exists using os module


I'm trying to validate if a directory received as user input exists using the os module

This is how I'm accepting the input:

directory = input("Hi ! \n please type a directory, thanks !")

The idea is that I want to make sure the user will type an existing directory and nothing else


Solution

  • Have you read the docs for the os module?

    Check out the following two links:

    os.path.exists()

    Return True if path refers to an existing path.

    os.path.isdir()

    Return True if path is an existing directory.