Niklas R
Niklas R

Reputation: 16900

Behaviour of "C:" with ntpath module

CPython 3.4.1

>>> import ntpath as p
>>> p.isabs('C:')
False
>>> p.isabs('C:\\')
True
>>> p.join('C:', 'foo')
'C:foo'
>>> p.join('C:\\', 'foo')
'C:\\foo'
>>>

What I would have expected

>>> import ntpath as p
>>> p.isabs('C:')
True
>>> p.join('C:', 'foo')
'C:\\foo'
>>> # others the same
  1. Why is C: not considered absolute, but C:\ is?
  2. Why does ntpath.join() not add a slash between C: and foo?

Upvotes: 1

Views: 355

Answers (1)

bobince
bobince

Reputation: 536755

Why is C: not considered absolute

Because without the additional slash it means “the current directory of the C: drive” (each drive having its own current-directory in DOS/Windows):

C:\> cd Windows
C:\WINDOWS\> python
Python 2.7.11. (default, ...)
>>> import os
>>> os.listdir('C:')
['0.log', 'addins', 'AppPatch', ...

(This is the listing of the C:\Windows directory, not the root C:\.)

Why does ntpath.join() not add a slash between C: and foo?

Maybe you wanted the file foo in the C: drive's current directory.

Practical upshot: just because a path is not ‘absolute’ that doesn't mean it's relative to the actual current working directory. Similarly, \ is an absolute path, but still depends on the current working drive.

(And riscospath is even weirder; in general, POSIX is the only platform on which ‘absoluteness’ is a useful concept.)

Upvotes: 4

Related Questions