passing environment variable path to open command

Christian Heimes lists at cheimes.de
Fri Jun 11 05:35:15 EDT 2010


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




More information about the Python-list mailing list