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

N

Nulpum

I want to make sure that folder exists.

'2011-07-03' is really exists. but 'os.path.isdir' say false

Does anyone know why?


False
 
K

Kushal Das

2011/7/19 Nulpum said:
I want to make sure that folder exists.

'2011-07-03' is really exists. but 'os.path.isdir' say false

Does anyone know why?



False
Works here. Are you sure that it is not a file ?
True

Python 2.7.1 (r271:86832, Apr 12 2011, 16:15:16)
[GCC 4.6.0 20110331 (Red Hat 4.6.0-2)] on linux2


Kushal
 
M

Michael Hrivnak

What is the output of:

? One possible issue here is that for some reason os.path.isdir()
can't even access the directory either because of permissions,
misinterpretation of the path, or some other reason.

Michael
 
S

Steven D'Aprano

Nulpum said:
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.
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:
logs\2011-07-03


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


(3) Use another operating system. *wink*
 
R

Rob Williscroft

Nulpum wrote in (e-mail address removed) in gmane.comp.python.general:
I want to make sure that folder exists.
'2011-07-03' is really exists. but 'os.path.isdir' say false
Does anyone know why?

False

Maybe it isn't a directory, but a file, what does os.path.exists() return.

Also could it be a "Shortcut" in which case 2011-07-03.lnk will exist.

Also have you left "Hide extensions for known file types" switched on,
in which case it may really be "2011-07-03.zip" for example, a file
not a directory even though Windows explorer shows it as a directory.
 
T

Thomas 'PointedEars' Lahn

Steven said:
logs�1-07-03

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

Besides that, and permission issues, ISTM that there are Unicode characters
(at least non-ASCII characters) in the OP's path, which means they should
declare

# encoding: utf-8

or something else fitting, and use

os.path.isdir(u"C:/Users/조창준/Desktop/logs/2011-07-03")

or something else fitting.
 
T

Thomas Jollans

Nulpum said:
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.
logs�1-07-03

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

[snip]

There are three solutions:

(1) Escape every backslash with an extra backslash:
logs\2011-07-03

There is a more elegant solution: use raw strings: r'c:\foo\bar'
(2) Use forward slashes, as Windows will happily accept them instead of
backslashes.

The "correct" solution in many cases is to not assume any particular
path separator at all, and use os.path.join when dealing with paths.
This will work even on systems that do not accept forward slashes as
path separators. (does Python still support any of those?)
(3) Use another operating system. *wink*

This, of course, is the only truly tenable solution.

Thomas
 
N

Nulpum

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:


logs\2011-07-03

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

(3) Use another operating system. *wink*

Thank you very much, Steven
You're right.

Now. It's OK.

Thanks again.
 
S

Steven D'Aprano

Thomas said:
logs\2011-07-03

There is a more elegant solution: use raw strings: r'c:\foo\bar'[/QUOTE]

Well, perhaps, but not all paths can be written as a raw string:
File "<stdin>", line 1
path = r'a\b\c\'
^
SyntaxError: EOL while scanning single-quoted string

The "correct" solution in many cases is to not assume any particular
path separator at all, and use os.path.join when dealing with paths.
This will work even on systems that do not accept forward slashes as
path separators. (does Python still support any of those?)

Yes, but only just. Python still includes support for VMS, at least for now;
support is scheduled to be dropped in 3.3 and code supporting it to be
removed in 3.4.
 
C

Changjun

Besides that, and permission issues, ISTM that there are Unicode characters
(at least non-ASCII characters) in the OP's path, which means they should
declare

# encoding: utf-8

or something else fitting, and use

os.path.isdir(u"C:/Users/조창준/Desktop/logs/2011-07-03")

or something else fitting.

Thanks...
u Option is work :)
 
G

Grant Edwards

I want to make sure that folder exists.

'2011-07-03' is really exists. but 'os.path.isdir' say false

Does anyone know why?



False

You're not using backslashes corrrectly. Try assigning the path
names to a "variable" and printing them. I think you'll see what's
wrong.

I'd try using forward slashes if I were you.
 
E

Ethan Furman

Steven said:
Yes, but only just. Python still includes support for VMS, at least for now;
support is scheduled to be dropped in 3.3 and code supporting it to be
removed in 3.4.

Perhaps I misremember, but I thought somebody had stepped forward to
keep OpenVMS support going?

....

Ah, here it is -- Sandeep Mathew, in thread 'Python Support on OpenVMS'
on python-dev.

~Ethan~
 
T

Terry Reedy

Besides that, and permission issues, ISTM that there are Unicode characters
(at least non-ASCII characters) in the OP's path, which means they should
declare

# encoding: utf-8

That is the default in Py3. Not sure of OP specified what he used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top