File Handling Problems Python I/O

mhartl at post.harvard.edu mhartl at post.harvard.edu
Thu Jan 6 15:34:52 EST 2005


You can use the os module to build path names in a platform-independent
manner.  On my Linux box, I can type

>>> BASE = '/'
>>> import os
>>> os.path.join(BASE, 'foo', 'bar', 'baz')
'/foo/bar/baz'

On a Windows machine, you get

>>> BASE = 'C:'
>>> import os
>>> os.path.join(BASE, 'foo', 'bar', 'baz')
'C:\\foo\\bar\\baz'

This is a little more cumbersome that just typing the string, but using
os.path.join makes it easy to port your scripts to a new platform.
Check out http://docs.python.org/lib/module-os.path.html for more
information.
Cheers,

Michael

--
Michael Hartl
http://michaelhartl.org/




More information about the Python-list mailing list