passing environment variable path to open command

Mahmood Naderan nt_mahmood at yahoo.com
Fri Jun 11 08:29:02 EDT 2010


Thanks, both of the methods work fine:

 open(os.path.expandvars("${SOMETHING}/sc_1.sh"), "r")
OR
 infile = open(os.path.join(env, 'sc_1.sh'),"r")

// Naderan *Mahmood;




________________________________
From: Christian Heimes <lists at cheimes.de>
To: python-list at python.org
Sent: Fri, June 11, 2010 2:05:15 PM
Subject: Re: passing environment variable path to open command

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

-- 
http://mail.python.org/mailman/listinfo/python-list



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100611/e76ced53/attachment.html>


More information about the Python-list mailing list