Why is python no longer waiting for os.system to finish?

前端 未结 3 505
星月不相逢
星月不相逢 2020-12-11 03:29

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?).

def         


        
相关标签:
3条回答
  • 2020-12-11 04:13

    You could also replace the call to os.system with subprocess.check_call, and that will raise an exception if the command fails:

    import subprocess as subp
    subp.check_call(str(cline), shell=True)
    
    0 讨论(0)
  • 2020-12-11 04:14

    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:

    vi forPython.sh (write 'my command' into it)
    chmod +x forPython.sh
    

    (in Python script)

    os.system("./forPython.sh")
    

    This makes python wait for your process to finish.

    0 讨论(0)
  • 2020-12-11 04:19

    os.system does 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:

    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 Blast function, or try to handle it in another way.

    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 blast program 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:

    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.
    
    0 讨论(0)
提交回复
热议问题