简体   繁体   中英

Run terminal programs from python

I would like to run a python program that interacts with the terminal to run another program and waits for it to complete before moving on. I have tried:

os.system('intersectBed -a Mutations.bed -b Promoters.bed -wb >Mutations.in.Promoters.bed')

subprocess.call('intersectBed -a Mutations.bed -b Promoters.bed -wb >Mutations.in.Promoters.bed', shell=True)

Neither run as i would like them to. Is there a way to do this?

intersectBed is the program I wish to run. If i use

with open('Mutations.in.Promoters.bed','w') as f:
subprocess.call(['intersectBed','-a','Mutations.bed','-b','Promoters.bed', '-wb'], stdout=f)

It gives an error that no such file or directory exists. But if i put that command into the terminal it works perfectly. The intersectBed is in the /bin folder. Does that make a difference?

EDIT*

with open('Mutations.in.Promoters.bed','w') as f:
subprocess.call(['/usr/local/bin/intersectBed','-a','Mutations.bed','-b','Promoters.bed', '-wb'], stdout=f)

THIS WORKED

Try this:

with open('Mutations.in.Promoters.bed', 'w') as f:
    subprocess.call(['intersectBed', '-a', 'Mutations.bed', '-b', 'Promoters.bed', '-wb'], stdout=f)

Referring to the documentation of subprocess , the use of shell=True should be avoided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM