[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

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

Giampaolo Rodola'

2/1/2008 8:32:00 PM

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.
>>> import os
>>> os.path.isabs('/a')
True
>>> os.path.isabs('/')
True
>>> os.path
<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.
>>> import os
>>> os.path.isabs('/a')
False
>>> os.path.isabs('/')
False
>>> os.path
<module 'javapath' from 'C:\dist\Lib\javapath.py'>
>>>


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

Grant Edwards

2/1/2008 8:55:00 PM

0

On 2008-02-01, Giampaolo Rodola' <gnewsg@gmail.com> wrote:

> 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.

--
Grant Edwards grante Yow! I hope something GOOD
at came in the mail today so
visi.com I have a REASON to live!!

Steve Holden

2/1/2008 8:58:00 PM

0

Giampaolo Rodola' wrote:
> 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.
>>>> import os
>>>> os.path.isabs('/a')
> True
>>>> os.path.isabs('/')
> True
>>>> os.path
> <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.
>>>> import os
>>>> os.path.isabs('/a')
> False
>>>> os.path.isabs('/')
> False
>>>> os.path
> <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
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.hold...

Giampaolo Rodola'

2/1/2008 9:19:00 PM

0

On Feb 1, 9:55 pm, Grant Edwards <gra...@visi.com> wrote:
> On 2008-02-01, Giampaolo Rodola' <gne...@gmail.com> wrote:
>
> > 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:

>>> os.getcwd()
'C:\\Python25'
>>> os.path.realpath('\\')
'C:\\'
>>>


Grant Edwards

2/1/2008 9:31:00 PM

0

On 2008-02-01, Steve Holden <steve@holdenweb.com> wrote:

> 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".

--
Grant Edwards grante Yow! I have accepted
at Provolone into my life!
visi.com

Grant Edwards

2/1/2008 9:34:00 PM

0

On 2008-02-01, Giampaolo Rodola' <gnewsg@gmail.com> wrote:
> On Feb 1, 9:55 pm, Grant Edwards <gra...@visi.com> wrote:
>> On 2008-02-01, Giampaolo Rodola' <gne...@gmail.com> wrote:
>>
>> > 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:
>
> >>> os.getcwd()
> 'C:\\Python25'
> >>> os.path.realpath('\\')
> 'C:\\'
> >>>

You'll get identical results with "/".

--
Grant Edwards grante Yow! I always have fun
at because I'm out of my
visi.com mind!!!

Giampaolo Rodola'

2/1/2008 9:40:00 PM

0

On Feb 1, 10:34 pm, Grant Edwards <gra...@visi.com> wrote:
> On 2008-02-01, Giampaolo Rodola' <gne...@gmail.com> wrote:
>
>
>
>
>
> > On Feb 1, 9:55 pm, Grant Edwards <gra...@visi.com> wrote:
> >> On 2008-02-01, Giampaolo Rodola' <gne...@gmail.com> wrote:
>
> >> > 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:
>
> > >>> os.getcwd()
> >  'C:\\Python25'
> > >>> os.path.realpath('\\')
> >  '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:\>

Grant Edwards

2/1/2008 9:52:00 PM

0

On 2008-02-01, Giampaolo Rodola' <gnewsg@gmail.com> wrote:

>> >> 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:
>>
>> > >>> os.getcwd()
>> >  'C:\\Python25'
>> > >>> os.path.realpath('\\')
>> >  '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
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.]

--
Grant Edwards grante Yow! I just had a NOSE
at JOB!!
visi.com

Ross Ridge

2/2/2008 12:14:00 AM

0

Grant Edwards <grante@visi.com> wrote:
>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

--
l/ // Ross Ridge -- The Great HTMU
[oo][oo] rridge@csclub.uwaterloo.ca
-()-/()/ http://www.csclub.uwaterloo.c...
db //

Martin v. Loewis

2/2/2008 1:15:00 AM

0

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

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

Regards,
Martin