cross platform accessing paths (windows, linux ...)

Discussion in 'Python' started by Vlastimil Brom, Mar 12, 2009.

  1. Hi all,
    I'd like to ask for some advice on how to acomplish file access in a
    cross platform way.
    My application is a kind of viewer of text and corresponding image
    files (stored in separate subdirectories) and I'm going to deploy it
    as binaries for windows and source files (again in separate
    directories);
    Of course the text and image data should be shared for the source and
    executable version.
    The program should be runnable without instalation, e.g. directly from
    a CD-ROM, flashdisk etc. (supposing the working python ...
    installation while using the source version).
    the directory structure looks like:
    my_viewer
    - src
    - bin
    - txt
    - img

    While writing the source on windows, I didn't notice problems, as the
    simple path
    "../txt/text_1.txt"
    worked well with open(...). (script file executed with the associated
    python.exe)
    However on Linux (Kubuntu 8.0.4) the files in neighbour directories
    were not found most of the time (probably depending on how the script
    was run - console; file manager Krusader ...)
    After a lot of trials and gradually solving some corner issues, I
    ended up with:

    def path_from_pardir(path):
    return os.path.realpath(os.path.normpath(os.path.join(os.path.dirname(__file__),
    os.pardir, path)))
    # __file__ is substituted with sys.path[0] if not present

    real_path = path_from_pardir("txt/text_1.txt")

    The above seems to work both on windows and linux, but it very much
    looks like woodoo code for me;
    as I have rather limited experiences on Linux, I'd like to ask, how
    this would be best done in a cross platform way; or is the above realy
    the way to go?
    (I hope, using / as path separator should be fine, as it is also
    supported on windows and other OS would use the slash anyway; is it
    true, or do I have to use os.sep (which would complicate the code
    slightly more)?
    Are there any issues I'm likely to run into say on Mac with this approach?

    I'm using python 2.5.4 on windows XPp SP3, and the default python
    2.5.2 on Kubuntu 8.0.4;
    (as I probably can't reasonably use 2.6 here for deployment with py2exe).

    Any hints or comments are much appreciated; thanks in advance!

    regards,
    Vlasta
    Vlastimil Brom, Mar 12, 2009
    #1
    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. Oliver Bleckmann
    Replies:
    2
    Views:
    348
    Keith Thompson
    Nov 12, 2006
  2. Oliver Bleckmann
    Replies:
    6
    Views:
    442
    ondra.holub
    Nov 13, 2006
  3. Michael Reichenbach
    Replies:
    5
    Views:
    377
    Zeppe
    May 25, 2007
  4. Vlastimil Brom
    Replies:
    1
    Views:
    421
    David Smith
    Mar 13, 2009
  5. utabintarbo

    Cross-platform file paths

    utabintarbo, May 7, 2010, in forum: Python
    Replies:
    4
    Views:
    391
    Wolfgang Rohdewald
    May 9, 2010
Loading...

Share This Page