os.path.isdir do not work for Foder named '2011-07-03'

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Jul 19 00:42:26 EDT 2011


Nulpum wrote:

> I want to make sure that folder exists.
> 
> '2011-07-03' is really exists. but 'os.path.isdir' say false
> 
> Does anyone know why?

Yes.

>>> print "logs/2011-07-03"
logs/2011-07-03
>>> print "logs\2011-07-03"
logs�1-07-03

Don't use backslashes as path separators in Python. Backslashes are used for
string escapes.

\n means newline, not backslash n

\t means tab, not backslash t

and \201 means octal character 0201 (hex 'x81', decimal 129).

There are three solutions:

(1) Escape every backslash with an extra backslash:

>>> print "logs\\2011-07-03"
logs\2011-07-03


(2) Use forward slashes, as Windows will happily accept them instead of
backslashes.


(3) Use another operating system. *wink*



-- 
Steven




More information about the Python-list mailing list