Multiplatform scripts: Can I avoid os.sep?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gmax2006

    #1

    Multiplatform scripts: Can I avoid os.sep?

    Hi,

    I am developing scripts that must run on both Linux and windows.

    My scripts contain lots of relative paths (such as log\\log.txt or
    ctl\\table.ctl) If I use os.sep, it makes the code ugly. Is there any
    tips or techniques to have Python automatically converts \\ to / when
    the script runs on Linux? What is the best way to deal with this
    situation?

    Thank you,
    Max

  • Mike Kent

    #2
    Re: Multiplatform scripts: Can I avoid os.sep?

    gmax2006 wrote:
    Hi,
    >
    I am developing scripts that must run on both Linux and windows.
    >
    My scripts contain lots of relative paths (such as log\\log.txt or
    ctl\\table.ctl) If I use os.sep, it makes the code ugly. Is there any
    tips or techniques to have Python automatically converts \\ to / when
    the script runs on Linux? What is the best way to deal with this
    situation?
    Use Jason Orendorff's path module:

    http://www.jorendorff.com/articles/python/path/

    Then you can do things like:

    targetPath = 'some' / 'random' / 'path'

    and the overridden '/' operator will do the right thing for the OS you
    are on.
    Of course, using the '/' operator in this manner makes some people's
    heads explode.

    Comment

    • Bruno Desthuilliers

      #3
      Re: Multiplatform scripts: Can I avoid os.sep?

      gmax2006 wrote:
      Hi,
      >
      I am developing scripts that must run on both Linux and windows.
      >
      My scripts contain lots of relative paths (such as log\\log.txt or
      ctl\\table.ctl) If I use os.sep, it makes the code ugly. Is there any
      tips or techniques to have Python automatically converts \\ to / when
      the script runs on Linux? What is the best way to deal with this
      situation?
      os.path.join('l og', 'log.txt')
      os.path.join('c tl', 'table.ctl')

      Or look for the 'path' module
      http://www.jorendorff.com/articles/python/path/

      HTH
      --
      bruno desthuilliers
      python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
      p in 'onurb@xiludom. gro'.split('@')])"

      Comment

      • Bruno Desthuilliers

        #4
        Re: Multiplatform scripts: Can I avoid os.sep?

        Mike Kent wrote:
        (snip - about Jason Orendorff's path module)
        Of course, using the '/' operator in this manner makes some people's
        heads explode.
        +1 QOTW


        --
        bruno desthuilliers
        python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
        p in 'onurb@xiludom. gro'.split('@')])"

        Comment

        • Fredrik Lundh

          #5
          Re: Multiplatform scripts: Can I avoid os.sep?

          gmax2006 wrote:
          I am developing scripts that must run on both Linux and windows.
          >
          My scripts contain lots of relative paths (such as log\\log.txt or
          ctl\\table.ctl) If I use os.sep, it makes the code ugly. Is there any
          tips or techniques to have Python automatically converts \\ to / when
          the script runs on Linux? What is the best way to deal with this
          situation?
          forward slashes work just fine on Windows too, in almost all cases.

          (for the few cases when they don't work, use os.path.normpat h and
          friends to fix them up).

          </F>

          Comment

          Working...