linux - How to call a series of bash commands in python and store output -


i trying run following bash script in python , store readlist output. readlist want stored python list, list of files in current directory ending in *concat_001.fastq.

i know may easier in python (i.e.

import os readlist = [f f in os.listdir(os.getcwd()) if f.endswith("concat_001.fastq")] readlist = sorted(readlist) 

however, problematic, need python sort list in same bash, , finding bash , python sort things in different orders (eg python , bash deal capitalised , uncapitalised things differently - when tried

readlist = np.asarray(sorted(flist, key=str.lower)) 

i still found 2 files starting ml_ , m_ sorted in different order bash , python. hence trying run exact bash script through python, use sorted list generated bash in subsequent python code.

input_suffix="concat_001.fastq" ender=`echo $input_suffix | sed "s/concat_001.fastq/\*concat_001.fastq/g" ` readlist="$(echo $ender)" 

i have tried

proc = subprocess.call(command1, shell=true, stdout=subprocess.pipe) proc = subprocess.call(command2, shell=true, stdout=subprocess.pipe) proc = subprocess.popen(command3, shell=true, stdout=subprocess.pipe) 

but get: subprocess.popen object @ 0x7f31cfcd9190

also - don't understand difference between subprocess.call , subprocess.popen. have tried both.

thanks, ruth

so question little confusing , not explain want. however, i'll try give suggestions update it, or in effort, answer it.

i assume following: python script passing command line 'input_suffix' , want python program receive contents of 'readlist' when external script finishes.

to make our lives simpler, , allow things more complicated, make following bash script contain commands:

script.sh

#!/bin/bash input_suffix=$1 ender=`echo $input_suffix | sed "s/concat_001.fastq/\*concat_001.fastq/g"` readlist="$(echo $ender)" echo $readlist 

you execute script.sh "concat_001.fastq", $1 takes in first argument passed on command line.

to use python execute external scripts, quite rightly found, can use subprocess (or noted response, os.system - although subprocess recommended).

the docs tell subprocess.call:

"wait command complete, return returncode attribute."

and

"for more advanced use cases when these not meet needs, use underlying popen interface."

given want pipe output bash script python script, let's use popen suggested docs. posted other stackoverflow answer, following:

import subprocess subprocess import popen, pipe  # execute out script , pipe output stdout process = subprocess.popen(['script.sh', 'concat_001.fastq'],                             stdout=subprocess.pipe,                             stderr=subprocess.pipe)  # obtain standard out, , standard error stdout, stderr = process.communicate() 

and then:

>>> print stdout *concat_001.fastq 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -