7

Why the first // are not removed ?

The following code:

import os
os.path.normpath('//var//lib/')

returns

'//var/lib'

not

'/var/lib'

Here the definition:

normpath(path)
    '''Normalize path, eliminating double slashes, etc.'''
Nouman
  • 6,947
  • 7
  • 32
  • 60
user2652620
  • 464
  • 2
  • 17

1 Answers1

6

Because on Windows, there is a path ambiguity that python preserves.

//var/whatever could refer to a drive mounted as the name //var

OR

/var/whatever could refer to a local drive directory.

If python collapsed leading double slashes, you could unknowingly change a path to refer to a different location.

Another way of saying this is that //var and /var are fundamentally different paths, and python treats them differently. You should probably change your test case to reflect this.

NateTheGrate
  • 590
  • 3
  • 11
  • `//var` is only well-defined to be a remote filesystem reference on Windows, *but* `//foo` is allowed to be given local/special handling on UNIX too (see last sentence of the spec at http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap04.html#tag_04_11). It *isn't* given that handling on most out-of-the-box UNIX systems, but filesystems like AFS where it's special do exist. – Charles Duffy Sep 10 '18 at 15:08
  • Didn't know that! OP seems to be on windows, but that's an important edge case. – NateTheGrate Sep 10 '18 at 15:11