[Python-Dev] PEP 355 status

Phillip J. Eby pje at telecommunity.com
Wed Oct 25 20:19:22 CEST 2006

At 10:16 AM 10/25/2006 -0700, Talin wrote:
>Phillip J. Eby wrote:
> > At 09:49 AM 10/25/2006 -0700, Talin wrote:
> >> Having done a path library myself (in C++, for our code base at work),
> >> the trickiest part is getting the Windows path manipulations right, and
> >> fitting them into a model that allows writing of platform-agnostic code.
> >> This is especially vexing when you realize that its often useful to
> >> manipulate unix-style paths even when running under Win32 and vice
> >> versa. A prime example is that I have a lot of Python code at work that
> >> manipulates Perforce client specs files. The path specifications in
> >> these files are platform-agnostic, and use forward slashes regardless of
> >> the host platform, so "os.path.normpath" doesn't do the right thing
> >> for me.
> >
> > You probably want to use the posixpath module directly in that case,
> > though perhaps you've already discovered that.
>Never heard of it. Its not in the standard library, is it? I don't see
>it in the table of contents or the index.

posixpath, ntpath, macpath, et al are the platform-specific path 
manipulation modules that are aliased to os.path.  However, each of these 
modules' string path manipulation functions can be imported and used on any 
platform.  See below:


Python 2.3.5 (#1, Aug 25 2005, 09:17:44)
[GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import os
 >>> os.path
<module 'posixpath' from '/usr/lib/python2.3/posixpath.pyc'>
 >>> import ntpath
 >>> dir(ntpath)
['__all__', '__builtins__', '__doc__', '__file__', '__name__', 'abspath', 
'altsep', 'basename', 'commonprefix', 'curdir', 'defpath', 'dirname', 
'exists', 'expanduser', 'expandvars', 'extsep', 'getatime', 'getctime', 
'getmtime', 'getsize', 'isabs', 'isdir', 'isfile', 'islink', 'ismount', 
'join', 'normcase', 'normpath', 'os', 'pardir', 'pathsep', 'realpath', 
'sep', 'split', 'splitdrive', 'splitext', 'splitunc', 'stat', 
'supports_unicode_filenames', 'sys', 'walk']


Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
 >>> import os
 >>> os.path
<module 'ntpath' from 'C:\Python23\lib\ntpath.pyc'>
 >>> import posixpath
 >>> dir(posixpath)
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '_varprog', 
'abspath', 'altsep', 'basename', 'commonprefix', 'curdir', 'defpath', 
'dirname', 'exists', 'expanduser', 'expandvars', 'extsep', 'getatime', 
'getctime', 'getmtime', 'getsize', 'isabs', 'isdir', 'isfile', 'islink', 
'ismount', 'join', 'normcase', 'normpath', 'os', 'pardir', 'pathsep', 
'realpath', 'samefile', 'sameopenfile', 'samestat', 'sep', 'split', 
'splitdrive', 'splitext', 'stat', 'supports_unicode_filenames', 'sys', 'walk']

Note, therefore, that any "path object" system should also allow you to 
create and manipulate foreign paths.  That is, it should have variants for 
each path type, rather than being locked to the local platform's path 
strings.  Of course, the most common need for this is manipulating posix 
paths on non-posix platforms, but sometimes one must deal with Windows 
paths on Unix, too.

More information about the Python-Dev mailing list