noob: subprocess clarification

Chris Rebert clp at rebertia.com
Sun Sep 14 11:48:40 CEST 2008


On Sun, Sep 14, 2008 at 2:29 AM,  <moogyd at yahoo.co.uk> wrote:
> Hi,
>
> I generally use csh scripts for generally scripting (controlling
> simulations). Basically the script processing options, generates the
> command line, executes it and then processes the results.
>
> I would usually use the -f option on the shebang line to ensure that
> the environment from the current shell is used.
>
> i.e. #!/bin/csh -f

According to my copy of the csh manpage:

−f   The shell ignores˜/.tcshrc, and thus starts faster.

So, if I understand correctly, this really just ensures that the rc
file isn't run again, thus preventing any environment vars from being
re-initialized and clobbered.
Since Python isn't (conventionally) a unix shell, this sort of thing
isn't an issue and using the current shell environment is the default
behavior. You don't need to do anything special.

>
> How do I acheive this in python?
>
> I have been looking at the
>
> import subprocess as sub
> p = sub.Popen(['echo $PATH'],shell=True).wait()
>
> This *seems* to do what I want (in that the path looks correct), but I
> don't really understand the documentation.
>
> Can somebody please clarify what the shell=True does, and whether I am
> using it correctly.

Basically, I think it runs the command string through the shell (i.e.
sh), thus causing shell syntax (e.g. $variables) to be interpreted. In
this particular case, $PATH is interpolated with the value of the PATH
environment variable.

You can also access environment variables using os.environ, for example:

import os
print os.environ['PATH']

Regards,
Chris

>
> Thanks,
>
> Steven
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

-- 
Follow the path of the Iguana...
http://rebertia.com


More information about the Python-list mailing list