os.path.split gets confused with combined \\ and /

Discussion in 'Python' started by Stef Mientki, May 17, 2009.

  1. Stef Mientki

    Stef Mientki Guest

    hello,

    just wonder how others solve this problem:
    I've to distribute both python files and data files.
    Everything is developed under windows and now the datafiles contains
    paths with mixed \\ and /.
    Under windows everthing is working well,
    but under Ubuntu / Fedora sometimes strange errors occurs.
    Now I was thinking that using os.path.split would solve all problems,
    but if I've the following relative path

    path1/path2\\filename.dat

    split will deliver the following under windows
    path = path1 / path2
    filename = filename.dat

    while under Linux it will give me
    path = path1
    filename = path\\filename.dat

    So I'm now planning to replace all occurences of os.path.split with a
    call to the following function

    def path_split ( filename ) :
    # under Ubuntu a filename with both
    # forward and backward slashes seems to give trouble
    # already in os.path.split
    filename = filename.replace ( '\\','/')

    return os.path.split ( filename )

    how do others solve this problem ?
    Are there better ways to solve this problem ?

    thanks,
    Stef Mientki
     
    Stef Mientki, May 17, 2009
    #1
    1. Advertising

  2. Stef Mientki wrote:
    > I've to distribute both python files and data files.
    > Everything is developed under windows and now the datafiles contains
    > paths with mixed \\ and /.


    For your info: Some (!!!) parts of MS Windows understand forward slashes as
    path separators and disallows them in file names, so you can often get away
    with mixed paths. However, no POSIX system will understand a backslash as
    path separator, which is a normal (though unusual) character in a file
    name.

    > Under windows everthing is working well,
    > but under Ubuntu / Fedora sometimes strange errors occurs.
    > Now I was thinking that using os.path.split would solve all problems,
    > but if I've the following relative path
    >
    > path1/path2\\filename.dat
    >
    > split will deliver the following under windows
    > path = path1 / path2
    > filename = filename.dat
    >
    > while under Linux it will give me
    > path = path1
    > filename = path\\filename.dat
    >
    > So I'm now planning to replace all occurences of os.path.split with a
    > call to the following function
    >
    > def path_split ( filename ) :
    > # under Ubuntu a filename with both
    > # forward and backward slashes seems to give trouble
    > # already in os.path.split
    > filename = filename.replace ( '\\','/')
    >
    > return os.path.split ( filename )
    >
    > how do others solve this problem ?
    > Are there better ways to solve this problem ?


    Your data files must not contain OS-dependent paths. So, I would recommend
    that you simply use forward slashes exclusively there, because those are
    most common as path separators. Under POSIX systems like Ubuntu, you can
    then use them 'as-is', while under win32 you would first convert them to
    paths with backslashes (os.path.separator). Generalised, you could do
    something like this:

    indep_path = string.split(line, data_path_separator)
    native_path = os.path.join(indep_path)

    'data_path_separator' would then of course have to be the type of path
    separator that is defined for your data format, per above I would suggest
    slashes.

    Uli


    --
    Sator Laser GmbH
    Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
     
    Ulrich Eckhardt, May 18, 2009
    #2
    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. Replies:
    2
    Views:
    490
  2. Stefan Mueller
    Replies:
    5
    Views:
    549
    Steven Saunderson
    Jul 10, 2006
  3. John Joyce

    gets gets

    John Joyce, Mar 26, 2007, in forum: Ruby
    Replies:
    2
    Views:
    380
    John Joyce
    Mar 26, 2007
  4. John Joyce

    Return of gets gets

    John Joyce, Apr 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    215
    John Joyce
    Apr 23, 2007
  5. libsfan01
    Replies:
    5
    Views:
    272
    Jeff North
    Dec 20, 2006
Loading...

Share This Page