[Python-ideas] os.path.join

Antoine Pitrou solipsis at pitrou.net
Fri Nov 29 18:54:25 CET 2013


On Wed, 30 Oct 2013 10:06:03 -0700
Bruce Leban <bruce at leapyear.org> wrote:
> 
> I agree it might be confusing but it's pretty explicitly documented. On the
> other hand, this is also documented and it's wrong by the above standard
> 
> >>> os.path.join(r'c:\abc', r'\def\g')   # Windows paths
> '\\def\\g'
> 
> On Windows \def\g is a drive-relative path not an absolute path. To get the
> right result you need to do:
> 
> >>> drive, path = os.path.splitdrive(r'c:\abc')
> >>> drive + os.path.join(path, r'/def/g')
> 'c:/def/g'

Note that pathlib gets it right:

>>> PureWindowsPath(r'c:\abc') / r'\def\g'
PureWindowsPath('c:/def/g')

>>> PureWindowsPath(r'\\abc\def\ghi') / r'\x\y'
PureWindowsPath('//abc/def/x/y')

Regards

Antoine.




More information about the Python-ideas mailing list