From: Jean-Michel Pichavant on
Mahmood Naderan wrote:
>
> Hi,
> I am new to python so my question may be very basic.
> Suppose I have a file (sc_1.sh) which the path to that file is in
> system path:
>
> SOMETHING=/home/mahmood/scripts
>
>
>
> Now I want to open that file with respect to the environment variable:
> import os
> env = os.getenv("SOMETHING")
> print env
>
> infile = open("env/sc_1.sh","r")
>
>
>
> But here is the error I get:
> /home/mahmood/scripts
> Traceback (most recent call last):
> File "test.py", line 7, in <module>
> infile = open("env/sc_1.sh","r")
> IOError: [Errno 2] No such file or directory: 'env/sc_1.sh'
>
>
>
> How can I use such variable in open file command?
>
> Thanks,
>
>
> *// Naderan *Mahmood;*
>
import os

infile = open(os.path.join(env, 'sc_1.sh'),"r")

JM
From: Christian Heimes on
Am 11.06.2010 10:39, schrieb Mahmood Naderan:
> Hi,
> I am new to python so my question may be very basic.
> Suppose I have a file (sc_1.sh) which the path to that file is in system path:
> SOMETHING=/home/mahmood/scripts
>
> Now I want to open that file with respect to the environment variable:
> import os
> env = os.getenv("SOMETHING")
> print env
> infile = open("env/sc_1.sh","r")
>
> But here is the error I get:
> /home/mahmood/scripts
> Traceback (most recent call last):
> File "test.py", line 7, in <module>
> infile = open("env/sc_1.sh","r")
> IOError: [Errno 2] No such file or directory: 'env/sc_1.sh'
>
> How can I use such variable in open file command?
> Thanks,

How about:

open(os.path.expandvars("${SOMETHING}/sc_1.sh"), "r")

See http://docs.python.org/library/os.path.html#os.path.expandvars