Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

In the os module in Python, is there a way to find if a directory exists, something like:

>>> os.direxists(os.path.join(os.getcwd()), 'new_folder')) # in pseudocode
True/False
share|improve this question
3  
A word of warning - the highest rated answer might be susceptible to race conditions. You might want to perform os.stat instead, to see if the directory both exists and is a directory at the same moment. – d33tah Feb 11 '14 at 17:09
    
@d33tah You may have a good point but I don't see a way to use os.stat to tell directory from a file. It raises OSError when the path is invalid, no matter whether it's file or directory. Also, any code after checking is also susceptible to race conditions. – Tomáš Zato Sep 7 '15 at 14:58
    
@TomášZato: which leads to a conclusion that it's safed to just perform the operation and handle errors. – d33tah Sep 7 '15 at 15:33
    
@David542 I added a clarification case with tests for precision for "isdir" "exists". I think you would learn anything now. But it could illuminate new people. – GeoStoneMarten Dec 2 '15 at 9:50
up vote 624 down vote accepted

You're looking for os.path.isdir, or os.path.exists if you don't care whether it's a file or a directory.

Example:

import os
print(os.path.isdir("/home/el"))
print(os.path.exists("/home/el/myfile.txt"))
share|improve this answer
3  
@syedrakib While parentheses can be used to indicate that an object is callable, that's not useful in Python, since even classes are callable. Also, functions are first-class values in Python, and you can use them without the parentheses notation, like in existing = filter(os.path.isdir(['/lib', '/usr/lib', '/usr/local/lib']) – phihag Mar 30 '13 at 7:38
7  
You can pass functions to other functions, like map, but in the general case, you call functions with arguments and parentheses. Also, there is some typo in your example. presumably you mean filter(os.path.isdir, ['/lib', '/usr/lib', '/usr/local/lib']). – hughdbrown Mar 31 '13 at 23:02

Yes, use os.path.exists().

share|improve this answer
10  
That doesn't check that the path is a directory. – Kirk Strauser Jan 19 '12 at 21:08
3  
Good call. Others have pointed out that os.path.isdir will accomplish that. – aganders3 Jan 19 '12 at 21:13

So close! os.path.isdir returns True if you pass in the name of a directory that currently exists. If it doesn't exist or it's not a directory, then it returns False.

share|improve this answer

Yes use os.path.isdir(path)

share|improve this answer

As in:

In [3]: os.path.exists('/d/temp')
Out[3]: True

Probably toss in a os.path.isdir(...) to be sure.

share|improve this answer

os provides you with a lot of these capabilities:

import os
os.path.isdir(dir_in) #True/False: check if this is a directory
os.listdir(dir_in)    #gets you a list of all files and directories under dir_in

the listdir will throw an exception if the input path is invalid.

share|improve this answer

Just to provide the os.stat version (python 2):

import os, stat, errno
def CheckIsDir(directory):
  try:
    return stat.S_ISDIR(os.stat(directory).st_mode)
  except OSError, e:
    if e.errno == errno.ENOENT:
      return False
    raise
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.