Converting from shell to python

Rita rmorgan466 at gmail.com
Mon Feb 21 11:33:26 EST 2011


Hello,

I have been using shell for a "long" time and I decided to learn python
recently. So far I am liking it a lot especially the argparse module which
makes my programs more professional like.


Currently, I am rewriting my bash scripts to python so I came across a
subprocess and environment problem.

My bash script looks like this,

#!/usr/bin/env bash

export JAVA_HOME="/opt/java"
export  PROG="dbconn"
export JAVA_ARGS="-Xmx16g"
export ARGS="-out outdata.dat"

$JAVA_HOME $JAVA_ARGS $PROG $ARGS


To convert this into python I did something like this (which isn't working
properly)

#!/usr/bin/env python
import subprocess,os
def execute():
  try:

    cmd="$JAVA_HOME $JAVA_ARGS $PROG $ARGS"
    cmd=cmd.split()

p=subprocess.Popen([cmd],env={"JAVA_HOME":"/opt/java","PROG":"dbconn","JAVA_ARGS":"-Xmx16g","ARGS":"-out
outdata.dat"},
         stdout=subprocess.PIPE)
  except OSError, e:
   print >>sys.stderr," Execution failed: ",e
 return p.stdout


So, I was wondering if this is the correct way of doing it? or is there an
alternative?










-- 
--- Get your facts first, then you can distort them as you please.--
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110221/6a242bd0/attachment.html>


More information about the Python-list mailing list