os.path.split: processing paths from one OS on another

Richie Hindle richie at entrian.com
Tue Jan 27 17:52:32 CET 2004

Hi Martijn,

> My server needs to be run on either Linux or Windows, it receives 
> requests from clients that may run either OS. To process those requests 
> i need to split the path and filename. I hoped to solves this using 
> os.path.split (), any suggestions as to how to fix this?

Explicitly use ntpath for Windows clients and posixpath for Linux clients:

>>> import ntpath, posixpath
>>> print ntpath.split('C:\\TEST\\FILE.EXT')
('C:\\TEST', 'FILE.EXT')
>>> print posixpath.split('/TEST/FILE.EXT')

Alternatively, change your client to always use forward slashes - then it
(apparently) doesn't matter which you use:

>>> print posixpath.split('c:/test/file.exe')
('c:/test', 'file.exe')
>>> print ntpath.split('c:/test/file.exe')
('c:/test', 'file.exe')
>>> print posixpath.split('//server/share/file.exe')
('//server/share', 'file.exe')
>>> print ntpath.split('//server/share/file.exe')
('//server/share', 'file.exe')

Beware - there may well be cases where they differ.

Windows itself is happy using forward slashes as a directory separator, so
your client can use them throughout.  It's only the Windows shell that
requires backslash rather than forward slash.

Richie Hindle
richie at entrian.com

More information about the Python-list mailing list