Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.path.isdir() returns False even when folder exists

I'm currently writing a script which has to check if all specified folders actually exist. I found out I have to use os.path.isdir() with absolute paths.

I have the following directory structure:

X:\
  pythonscripts\
    files\
      Films\
      Series\
    src\

When I open op my python command line and try if the folders actually exist, I get the following:

>>> import os
>>> os.path.isdir('X:\pythonscripts\src')
True
>>> os.path.isdir('X:\pythonscripts\files')
False
>>> os.path.isdir('X:\pythonscripts\files\Films')
False
>>> os.path.isdir('X:\pythonscripts\files\Series')
False

Which is odd, because when I copy and paste these paths into Windows Explorer, I can access them without problems. I checked permissions and all folders have the same permissions on them. Does anyone know what I'm doing wrong?

like image 964
Erik van de Locht Avatar asked Aug 18 '13 14:08

Erik van de Locht


2 Answers

Escape backslash (\)

os.path.isdir('X:\\pythonscripts\\src')

or use raw string:

os.path.isdir(r'X:\pythonscripts\src')

without escape, you get wrong path:

>>> '\f'
'\x0c'
>>> print '\f'

>>> print '\\f'
\f
>>> print r'\f'
\f
like image 180
falsetru Avatar answered Oct 21 '22 05:10

falsetru


Rather than use \, you might want to use the os.path.sep so that your code works on other platforms, then you don't have to escape these either.

like image 20
user3200272 Avatar answered Oct 21 '22 03:10

user3200272