2014-02-07 101 views
0

我想运行一个python程序,该程序与终端进行交互以运行另一个程序,并在继续前等待它完成。我曾尝试过:从python运行终端程序

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) 

既没有按照我希望的方式运行,有没有办法做到这一点?

intersectBed是我希望运行的程序。如果我使用

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

它给出了一个错误,即没有这样的文件或目录存在。但是,如果我把这个命令放入终端,它就能完美地工作。 intersectBed位于/ bin文件夹中。这有什么区别吗?

编辑*

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

这个工作

+0

当你运行这些命令时会发生什么?你得到什么错误信息? – pseudocubic

+0

我没有收到错误消息。它运行。它只是创建一个名为Mutations.in.Promoters.bed的空白文件。这个创建的文件应该是大约2GB,而不是0KB。 – user3266890

+0

是否指定''intersectBed''的完整路径? –

回答

1

试试这个:

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

参考的subprocessdocumentation,应避免使用shell=True