Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

python - 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:pythonscriptssrc')
True
>>> os.path.isdir('X:pythonscriptsfiles')
False
>>> os.path.isdir('X:pythonscriptsfilesFilms')
False
>>> os.path.isdir('X:pythonscriptsfilesSeries')
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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Escape backslash ()

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

or use raw string:

os.path.isdir(r'X:pythonscriptssrc')

without escape, you get wrong path:

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

>>> print '\f'
f
>>> print r'f'
f

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...