Differences between revisions 1 and 2
Revision 1 as of 2005-04-14 19:11:33
Size: 813
Editor: 168-103-146-113
Comment: environment variablse, popen,...
Revision 2 as of 2005-09-27 00:02:37
Size: 821
Editor: 63
Comment: no trailing newlines
Deletions are marked like this. Additions are marked like this.
Line 17: Line 17:
for line in os.popen("bash -c 'env'").readlines(): for line in os.popen("bash -c 'env'").read().splitlines():

These are just some notes about working with processes in Python.

The module of interest is os. [http://www.python.org/doc/current/lib/module-os.html (os module documentation)]

Environment variables are accessed through a dictionary, os.environ.

Processes are created with os.popen, [http://www.python.org/doc/current/lib/os-newstreams.html#os-newstreams described in os 6.1.2.]

   1 import os
   2 
   3 # Export an environment variable
   4 os.environ["FOO"] = "BAR"
   5 
   6 # Make sure environment variable set for child processes
   7 for line in os.popen("bash -c 'env'").read().splitlines():
   8     if line.startswith("FOO="):
   9         print line
  10 
  11 # Since environment variable "FOO" is exported, and since child
  12 # processes inherit environment variables from their parents, this
  13 # works.

WorkingWithProcesses (last edited 2008-11-15 14:01:10 by localhost)

Unable to edit the page? See the FrontPage for instructions.