By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,547 Members | 1,429 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,547 IT Pros & Developers. It's quick & easy.

Multiplatform scripts: Can I avoid os.sep?

P: n/a
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

Jul 13 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

Jul 13 '06 #2

P: n/a
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('log', 'log.txt')
os.path.join('ctl', '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 'o****@xiludom.gro'.split('@')])"
Jul 13 '06 #3

P: n/a
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 'o****@xiludom.gro'.split('@')])"
Jul 13 '06 #4

P: n/a
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.normpath and
friends to fix them up).

</F>

Jul 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.