caesar
caesar

Reputation: 3125

Detecting whether a folder is in a directory or not.

I want to make sure a folder whose name is known is in a specific directory or not. the directory that I want to check is my git repository. Assume that my git repository directory is /home/cmp/Desktop/GIT_REFERENCE_REPOSITORIES and I want to detect the folder namely 'test' is in there or not.

Is there anyone who can help me ? Thanks in advance!

Upvotes: 0

Views: 118

Answers (2)

Lohrun
Lohrun

Reputation: 6752

What your looking for is os.path.exists(path) and os.path.isdir(path) to check if a particular path exists and points to a directory.

Upvotes: 2

amadain
amadain

Reputation: 2836

Try this:

for root, dirs, files in os.walk('/home/cmp/Desktop/GIT_REFERENCE_REPOSITORIES'):
   dirs[:] = [os.path.join(root, d) for d in dirs]

for dir in dirs:
    if dir.endswith("test"):
         print "test is in dirs"

Upvotes: 1

Related Questions