为什么python不再等待os.system完成?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14059558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is python no longer waiting for os.system to finish?
提问by Stylize
I have the following function, which has been working great for months. I have not updated my version of Python (unless it happens behind the scenes?).
我有以下功能,几个月来一直很好用。我还没有更新我的 Python 版本(除非它发生在幕后?)。
def Blast(type, protein_sequence, start, end, genomic_sequence):
result = []
M = re.search('M', protein_sequence)
if M:
query = protein_sequence[M.start():]
temp = open("temp.ORF", "w")
print >>temp, '>blasting'
print >>temp, query
temp.close()
cline = blastp(query="'temp.ORF'", db="DB.blast.txt",
evalue=0.01, outfmt=5, out=type + ".BLAST")
os.system(str(cline))
blast_out = open(type + ".BLAST")
string = str(blast_out.read())
DEF = re.search("<Hit_def>((E|L)\d)</Hit_def>", string)
I receive the error that blast_out=open(type+".BLAST")cannot find the specified file. This file gets created as part of the output of the program called by the os.systemcall. This usually takes ~30s or so to complete. However, When I try to run the program, it instantly gives the error I mention above.
我收到blast_out=open(type+".BLAST")找不到指定文件的错误。该文件是作为调用所os.system调用程序输出的一部分创建的。这通常需要大约 30 秒左右才能完成。但是,当我尝试运行该程序时,它立即给出了我上面提到的错误。
I thought os.system()was supposed to wait for completion?
Should I force the wait somehow? (I do not want to hard code the wait time).
我以为os.system()是应该等待完成?
我应该以某种方式强制等待吗?(我不想硬编码等待时间)。
EDIT: I have ran the cline output in the command line version of the BLAST program. Everything appears to be fine.
编辑:我已经在 BLAST 程序的命令行版本中运行了 cline 输出。一切似乎都很好。
回答by Roland Smith
os.systemdoes wait. But there could be an error in the program called by it, so the file isn't created. You should check the return value of the called program before proceeding. In general, programs are supposed to return 0 when they finish normally, and another value when there is an error:
os.system确实等待。但是它调用的程序中可能存在错误,因此不会创建该文件。在继续之前,您应该检查被调用程序的返回值。一般来说,程序应该在正常完成时返回 0,在出现错误时返回另一个值:
if os.system(str(cline)):
raise RuntimeError('program {} failed!'.format(str(cline)))
blast_out=open(type+".BLAST")
Instead of raising an exception, you could also return from the Blastfunction, or try to handle it in another way.
除了引发异常,您还可以从Blast函数返回,或尝试以其他方式处理它。
Update:Wether the called program runs fine from the command line only tells you that there is nothing wrong with the program itself. Does the blastprogram return useful errors or messages when there is a problem? If so, consider using subprocess.Popen()instead of os.system, and capture the standard output as well:
更新:被调用的程序是否从命令行正常运行只会告诉您程序本身没有任何问题。blast当出现问题时,程序是否返回有用的错误或消息?如果是这样,请考虑使用subprocess.Popen()而不是os.system,并捕获标准输出:
prog = subprocess.Popen(cline, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = prog.communicate()
# Now you can use `prog.returncode`, and inspect the `out` and `err`
# strings to check for things that went wrong.
回答by Roland Smith
You could also replace the call to os.system with subprocess.check_call, and that will raise an exception if the command fails:
您还可以使用 subprocess.check_call 替换对 os.system 的调用,如果命令失败,则会引发异常:
import subprocess as subp
subp.check_call(str(cline), shell=True)
回答by Magdalena
This answer is a bit late. However, I had the same problem and subprocess didn't seem to work. I solved it by writing the command into a bash-file and executing the bash-file via python os.system:
这个回答有点晚了。但是,我遇到了同样的问题,并且子流程似乎不起作用。我通过将命令写入 bash 文件并通过 python os.system 执行 bash 文件来解决它:
vi forPython.sh (write 'my command' into it)
chmod +x forPython.sh
(in Python script)
(在 Python 脚本中)
os.system("./forPython.sh")
This makes python wait for your process to finish.
这使 python 等待您的进程完成。

