user3266890
user3266890

Reputation: 555

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

Upvotes: 0

Views: 339

Answers (1)

YS-L
YS-L

Reputation: 14748

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.

Upvotes: 1

Related Questions