file delimiter in dependency of os

Discussion in 'Python' started by Thomas Steffen, Nov 2, 2003.

  1. Hallo,

    I have pathes with different file delimiters
    e.g.
    /root/dir\mydir/myfile
    C:\xxx/hh\gggg/myfile

    How can I change the mistaken file delimiters in dependency of the
    used os (win or unix) effectively?
    unix: /root/dir\mydir --> /root/dir/mydir
    win: C:\xxx/hh\gggg/myfile --> C:\xxx\hh\gggg\myfile

    Thanks for your hints Thomas
    Thomas Steffen, Nov 2, 2003
    #1
    1. Advertising

  2. Thomas Steffen

    Peter Otten Guest

    Thomas Steffen wrote:

    > Hallo,
    >
    > I have pathes with different file delimiters
    > e.g.
    > /root/dir\mydir/myfile
    > C:\xxx/hh\gggg/myfile
    >
    > How can I change the mistaken file delimiters in dependency of the
    > used os (win or unix) effectively?
    > unix: /root/dir\mydir --> /root/dir/mydir
    > win: C:\xxx/hh\gggg/myfile --> C:\xxx\hh\gggg\myfile
    >
    > Thanks for your hints Thomas


    Note that backslashes are only needed for the conventional user experience,
    Windows is happy with "/", too. Have a look at os.path.normcase() which
    should meet your needs if you are consistently using forward slashes in
    your application. Otherwise the following tiny module might do (untested):

    <plat.py>
    import os
    if os.name == "posix":
    def normslash(path):
    return path.replace("\\", "/")
    elif os.name == "nt":
    def normslash(path):
    return path.replace("/", "\\")
    else:
    raise Exception("OS not supported: %s" % os.name")
    </plat.py>

    usage:

    import plat
    path = plat.normslash(path)

    By the way, if you are building paths like so:

    path = folder + "/" + filename

    Use os.path.join() instead. It will automatically select the right separator
    for you.

    Peter
    Peter Otten, Nov 2, 2003
    #2
    1. Advertising

  3. Thomas Steffen

    Peter Hansen Guest

    Peter Otten wrote:
    >
    > Thomas Steffen wrote:
    >
    > > Hallo,
    > >
    > > I have pathes with different file delimiters
    > > e.g.
    > > /root/dir\mydir/myfile
    > > C:\xxx/hh\gggg/myfile
    > >
    > > How can I change the mistaken file delimiters in dependency of the
    > > used os (win or unix) effectively?
    > > unix: /root/dir\mydir --> /root/dir/mydir
    > > win: C:\xxx/hh\gggg/myfile --> C:\xxx\hh\gggg\myfile
    > >
    > > Thanks for your hints Thomas

    >
    > Note that backslashes are only needed for the conventional user experience,
    > Windows is happy with "/", too. Have a look at os.path.normcase() which


    Or, to be more practical, backslashes are needed for commands that are
    passed to the MS-DOS shell with os.system() and friends. Any other
    way of getting the paths to the OS should work with forward slashes.

    -Peter
    Peter Hansen, Nov 2, 2003
    #3
    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. john brown
    Replies:
    6
    Views:
    566
    Jim Gibson
    Oct 22, 2003
  2. Mark Fox

    Delimiter Split

    Mark Fox, Aug 11, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    3,326
    Chris R. Timmons
    Aug 11, 2003
  3. arvind
    Replies:
    1
    Views:
    303
    Andrew Thompson
    Sep 11, 2006
  4. ciapecki
    Replies:
    12
    Views:
    249
    John W. Kennedy
    Dec 11, 2006
  5. Replies:
    4
    Views:
    146
Loading...

Share This Page