open file in dir independently of operating system

J

Joerg Schuster

Hello,


I want to open the file 'configuration.smo' that is in directory dir.
Yet, I don't know on which os my program is being run. On Unix I would
say:

f = open(dir + '/configuration.smo', 'r')

What is the os-independent version of this line?

(I have read the manual of the module os, but I didn't see how to do
it.)


Jörg Schuster
 
A

Andrew Bushnell

I believe you want:

import os
f = open(os.path.join(dir, 'configuration.smo'), 'r')

....


Joerg said:
Hello,


I want to open the file 'configuration.smo' that is in directory dir.
Yet, I don't know on which os my program is being run. On Unix I would
say:

f = open(dir + '/configuration.smo', 'r')

What is the os-independent version of this line?

(I have read the manual of the module os, but I didn't see how to do
it.)


Jörg Schuster

--
************************************
Andrew Bushnell
Lead Development Engineer
Fluent Inc.
10 Cavendish Court
Centerra Resource Park
Lebanon, NH 03766
(e-mail address removed)
Phone: 603-643-2600, ext. 757
Fax: 603-643-1721
www.fluent.com
************************************
 
G

Gerald Klix

Hi,
it`s

import os
f = open( os.path.join( dir , 'configuration.smo' ), 'r' )

HTH,
Gerald
 
T

Thomas Heller

Gerald said:
Hi,
it`s

import os
f = open( os.path.join( dir , 'configuration.smo' ), 'r' )

*nix-heads everywhere?

For a text file, I would prefer:
f = open( os.path.join( dir , 'configuration.smo' ), 'U' )

and for a binary file:
f = open( os.path.join( dir , 'configuration.smo' ), 'rb' )

That way it also works on Windows.

Thomas
 
K

Kay Schluehr

Joerg said:
Hello,


I want to open the file 'configuration.smo' that is in directory dir.
Yet, I don't know on which os my program is being run. On Unix I would
say:

f = open(dir + '/configuration.smo', 'r')

What is the os-independent version of this line?

Did You check this out on Windows? My experience with *nix paths is
that they work.
<open file 'C:/bla.txt', mode 'r' at 0x026B4A20>

Kay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,240
Messages
2,571,205
Members
47,843
Latest member
eicamotu

Latest Threads

Top