Note that there are some explanatory texts on larger screens.

plurals
  1. POExecuting shell program in Python without printing to screen
    text
    copied!<p>Is there a way that I can execute a shell program from Python, which prints its output to the screen, and read its output to a variable without displaying anything on the screen?</p> <p>This sounds a little bit confusing, so maybe I can explain it better by an example.</p> <p>Let's say I have a program that prints something to the screen when executed</p> <pre><code>bash&gt; ./my_prog bash&gt; "Hello World" </code></pre> <p>When I want to read the output into a variable in Python, I read that a good approach is to use the <code>subprocess</code> module like so:</p> <pre><code>my_var = subprocess.check_output("./my_prog", shell=True) </code></pre> <p>With this construct, I can get the program's output into <code>my_var</code> (here <code>"Hello World"</code>), however it is also printed to the screen when I run the Python script. Is there any way to suppress this? I couldn't find anything in the <code>subprocess</code> documentation, so maybe there is another module I could use for this purpose?</p> <p>EDIT: I just found out that <code>commands.getoutput()</code> lets me do this. But is there also a way to achieve similar effects in <code>subprocess</code>? Because I was planning to make a Python3 version at some point.</p> <hr> <p>EDIT2: Particular Example</p> <p>Excerpt from the python script:</p> <pre><code>oechem_utils_path = "/soft/linux64/openeye/examples/oechem-utilities/"\ "openeye/toolkits/1.7.2.4/redhat-RHEL5-g++4.3-x64/examples/"\ "oechem-utilities/" rmsd_path = oechem_utils_path + "rmsd" for file in lMol2: sReturn = subprocess.check_output("{rmsd_exe} {rmsd_pars}"\ " -in {sIn} -ref {sRef}".format(rmsd_exe=sRmsdExe,\ rmsd_pars=sRmsdPars, sIn=file, sRef=sReference), shell=True) dRmsds[file] = sReturn </code></pre> <hr> <p>Screen Output (Note that not "everything" is printed to the screen, only a part of the output, and if I use <code>commands.getoutput</code> everything works just fine:</p> <pre><code>/soft/linux64/openeye/examples/oechem-utilities/openeye/toolkits/1.7.2.4/redhat-RHEL5-g++4.3-x64/examples/oechem-utilities/rmsd: mols in: 1 out: 0 /soft/linux64/openeye/examples/oechem-utilities/openeye/toolkits/1.7.2.4/redhat-RHEL5-g++4.3-x64/examples/oechem-utilities/rmsd: confs in: 1 out: 0 /soft/linux64/openeye/examples/oechem-utilities/openeye/toolkits/1.7.2.4/redhat-RHEL5-g++4.3-x64/examples/oechem-utilities/rmsd - RMSD utility [OEChem 1.7.2] /soft/linux64/openeye/examples/oechem-utilities/openeye/toolkits/1.7.2.4/redhat-RHEL5-g++4.3-x64/examples/oechem-utilities/rmsd: mols in: 1 out: 0 /soft/linux64/openeye/examples/oechem-utilities/openeye/toolkits/1.7.2.4/redhat-RHEL5-g++4.3-x64/examples/oechem-utilities/rmsd: confs in: 1 out: 0 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload