Convert between Windows style paths and POSIX style paths

Discussion in 'Python' started by Noah, Jul 10, 2003.

  1. Noah

    Noah Guest

    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.

    Yours,
    Noah
    Noah, Jul 10, 2003
    #1
    1. Advertising

  2. Noah

    Peter Hansen Guest

    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?

    -Peter
    Peter Hansen, Jul 10, 2003
    #2
    1. Advertising

  3. Noah

    Cliff Wells Guest

    On Thu, 2003-07-10 at 13:44, 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.


    Just use POSIX paths and let the Python library sort it out. It works.
    If you really need to do the conversions yourself, take a look at the
    os.path module.

    --
    Cliff Wells, Software Engineer
    Logiplex Corporation (www.logiplex.net)
    (503) 978-6726 (800) 735-0555
    Cliff Wells, Jul 10, 2003
    #3
  4. "Peter Hansen" <> wrote in message
    news:...
    > 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"

    National Institute for Water and Atmospheric Research (NIWA)
    Mark Hadfield, Jul 10, 2003
    #4
  5. Noah

    N.K Guest

    os.path.join is what you are looking for, i beleive

    eg: appendedpath = os.path.join(dir1,filename)

    Regards
    Nirmal

    (Noah) wrote in message news:<>...
    > 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.
    >
    > Yours,
    > Noah
    N.K, Jul 11, 2003
    #5
  6. Noah

    Noah Guest

    Peter Hansen <> wrote in message news:<>...

    > 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?
    > -Peter


    I had forgotten that NT supports forward slashes.
    My only concern is that some of the config file values are
    set programatically and I use os.path.join everywhere, so if the
    config file is modified when running under Windows then the paths
    will get \ separators and that will confuse the UNIX side.
    I think I will still need something to ensure that the paths
    are consistent... Hmmm... I wonder how evil it would be to simply set
    os.sep = '/' when I initialize my application. I wonder if I will be
    angering the dark lords of Microsoft by doing this.

    I was going to simply remove the drive letter, so, yes, that would
    disallow paths other than the curent drive. I might also map the drive
    letters to a point on the POSIX path or I might do it Cygwin style
    where C: becomes /cygdrive/c/.
    Noah, Jul 11, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chaman Singh

    STL map and Posix Threads

    Chaman Singh, Apr 8, 2004, in forum: C++
    Replies:
    3
    Views:
    539
    Nick Hounsome
    Apr 9, 2004
  2. Leonard J. Reder

    POSIX library for Windows XP

    Leonard J. Reder, Apr 13, 2006, in forum: C Programming
    Replies:
    3
    Views:
    635
    Erik de Castro Lopo
    Apr 13, 2006
  3. asit

    portable C, posix C and windows C

    asit, Oct 8, 2008, in forum: C Programming
    Replies:
    22
    Views:
    1,007
    Ian Collins
    Oct 10, 2008
  4. Kevin
    Replies:
    2
    Views:
    117
    Kevin Olbrich
    Aug 11, 2005
  5. Ohad Lutzky

    Paths, gentleman, paths

    Ohad Lutzky, Nov 6, 2006, in forum: Ruby
    Replies:
    2
    Views:
    187
    David Vallner
    Nov 7, 2006
Loading...

Share This Page