Why os.path.isabs("/") on Windows returns True?

  • Thread starter Giampaolo Rodola'
  • Start date
G

Giampaolo Rodola'

Hi,
I'm trying to solve a jython-related issue and I discovered a
different behavior affecting os.path.isabs between CPython and Jython.


C:\Python23>python.exe
Python 2.3.5 (#62, Feb 8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.

C:\dist>jython.bat
Jython 2.3a0 on java1.6.0_04
Type "copyright", "credits" or "license" for more information.

Is there a reason why "/" is considered an absolute pathname by
CPython?
 
G

Grant Edwards

I'm trying to solve a jython-related issue and I discovered a
different behavior affecting os.path.isabs between CPython and
Jython.
[...]

Is there a reason why "/" is considered an absolute pathname
by CPython?

Sure seems like a bug to me. On Unix, "/" is an absolute path.
On windows, "/" is relative to the current device.
 
S

Steve Holden

Giampaolo said:
Hi,
I'm trying to solve a jython-related issue and I discovered a
different behavior affecting os.path.isabs between CPython and Jython.


C:\Python23>python.exe
Python 2.3.5 (#62, Feb 8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.<module 'ntpath' from 'C:\Python23\lib\ntpath.pyc'>


C:\dist>jython.bat
Jython 2.3a0 on java1.6.0_04
Type "copyright", "credits" or "license" for more information.<module 'javapath' from 'C:\dist\Lib\javapath.py'>


Is there a reason why "/" is considered an absolute pathname by
CPython?

Personally I'd say it was Jython that was wrong. Anything beginning with
a slash has to be absolute - "/" and "\" are treated as equivalent in
most parts of the Windows environment.

regards
Steve
 
G

Giampaolo Rodola'

I'm trying to solve a jython-related issue and I discovered a
different behavior affecting os.path.isabs between CPython and
Jython.
[...]

Is there a reason why "/" is considered an absolute pathname
by CPython?

Sure seems like a bug to me.  On Unix, "/" is an absolute path.
On windows, "/" is relative to the current device.

If you mean "the current drive letter" it's not actually true since "\
\" is used for that:
 
G

Grant Edwards

Personally I'd say it was Jython that was wrong. Anything
beginning with a slash has to be absolute - "/" and "\" are
treated as equivalent in most parts of the Windows
environment.

I guess it depends on your definition of "absolute". In my
mind, it means that it always refers to the same location
regardless of the CWD.

That's not true for the paths starting with "/" on Windows. If
CWD is "C:/foo/bar" then "/" refers to a different location
than it does if CWD is "D:/whatever".
 
G

Grant Edwards

I'm trying to solve a jython-related issue and I discovered a
different behavior affecting os.path.isabs between CPython and
Jython.
[...]

Is there a reason why "/" is considered an absolute pathname
by CPython?

Sure seems like a bug to me.  On Unix, "/" is an absolute path.
On windows, "/" is relative to the current device.

If you mean "the current drive letter" it's not actually true
since "\ \" is used for that:

You'll get identical results with "/".
 
G

Giampaolo Rodola'

I'm trying to solve a jython-related issue and I discovered a
different behavior affecting os.path.isabs between CPython and
Jython.
[...]
Is there a reason why "/" is considered an absolute pathname
by CPython?
Sure seems like a bug to me.  On Unix, "/" is an absolute path.
On windows, "/" is relative to the current device.
If you mean "the current drive letter" it's not actually true
since "\ \" is used for that:
 'C:\\'

You'll get identical results with "/".

Didn't know that. And this is another strange thing since Windows
shell does not behave like that:

C:\Python25>cd /

C:\Python25>cd \

C:\>
 
G

Grant Edwards

Didn't know that. And this is another strange thing since Windows
shell does not behave like that:

C:\Python25>cd /

C:\Python25>cd \

True, but we're not talking about how Windows' shell (cmd.exe)
parses its command line.

The Windows system calls (at least at the library level) are
perfectly happy with "/" as a directory separator and always
have been. MS-DOS system calls before that were also happy
with "/" as a directory separator. Back in the early days of
MS-DOS, you could even tell command.com to use '-' as an option
flag instead of '/', then you could use properly spelled paths
at the DOS shell also. [I've read that 32-bit cmd.exe has lost
that feature. I don't really know, I always use bash instead.]
 
R

Ross Ridge

Grant Edwards said:
I guess it depends on your definition of "absolute". In my
mind, it means that it always refers to the same location
regardless of the CWD.

Strictly speaking "/" refers to same location regardless of the current
working directory (CWD) on Windows. It's only relative with respect
of the current drive, which Windows considers something different than
the current working directory. On the other hand Python on Window
does for the most part hide this, treating the current drive as being
part of current working directory, so it's strange the isabs() behaves
differently.

Ross Ridge
 
M

Martin v. Löwis

Is there a reason why "/" is considered an absolute pathname by

Yes: it tests whether a path is absolute on the current volume.
Use the source, Luke.

Regards,
Martin
 

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
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top