Convert between Windows style paths and POSIX style paths

Mark Hadfield m.hadfield at niwa.co.nz
Fri Jul 11 00:53:48 CEST 2003


"Peter Hansen" <peter at engcorp.com> wrote in message
news:3F0DE565.1CEA39C6 at engcorp.com...
> Noah wrote:
> >
> > Does anyone have a function to convert back and forth between
> > NT style paths and POSIX style? It seems trivial, but
> > I want to make sure I don't overlook some obscure detail.
> > Is it a simple matter of translating / and \ characters?
> >
> > FYI, I need a Python function that does what cygpath does so that
> > I can run a script on either NT or UNIX or Cygwin.
> > I want my config files use one style of path.
>
> You can use forward slashes in paths under Win32, except at the
> command prompt.
>
> Even if you switch to use all forward slashes, however, what do
> you plan to do about drive letters?  There is no obvious mapping
> to anything under POSIX, I think.  Are you planning on disallowing
> paths that go anywhere but the current drive under NT?

Well, if you're running on an NT system with Cygwin installed, then the
obvious thing to do (really the only sensible thing to do IMHO) is to use
the mappings provided by Cygwin. These are set up in the registry and
accessed via commands like mount and cygpath, which call functions in
cygwin.dll. Eg my system has the following:

  D:\Cygwin <==> /
  D:\Local <==> /usr/local

And if you want to do *that* then the easiest way, though not the fastest,
is to run the cygpath command and trap the output. I have modules that do
this in Windows & Cygwin Python, if you (meaning the OP) are interested.
Another way is to access the registry and try to duplicate Cygwin's logic. I
tried this but gave up on it--life's too short for that. Or you try to adapt
the Cygwin C code. Or try to access the functions in cygwin.dll. Whatever.

On an NT system without Cygwin, or on a Unix system, then there is no
obvious mapping and (as far I can see) no need for one. Why do you want to
use one style of path in your config files? Surely it would be better to use
paths that are appropriate for the system, then process them with Python's
os module.

-- 
Mark Hadfield            "Ka puwaha te tai nei, Hoea tatou"
m.hadfield at niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)







More information about the Python-list mailing list