Search code examples
python-2.7os.path

os.path.isdir() gives always true


Hi I write a simple python code it will check folder/dir is exist or not in the /usr/share/* file but it's fail to check

import  os 
try:
     os.path.isdir('/usr/share/sqlmap')
     print 'sqlmap found'
except OSError:
     print "Sqlmap not found"
#Output
 sqlmap found 

Although it's not exist in my that directory. Please anyone tell me where my mistake is.


Solution

  • The documentation isn't very clear about this, but isdir would return False if the directory doesn't exist. It wouldn't throw an exception. Therefore it would be more appropriate for you to use an if-else block in this case.

    For example:

    import  os 
    if os.path.isdir('/usr/share/sqlmap'):
        print 'sqlmap found'
    else:
        print "Sqlmap not found"