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

python os.system don't wait
python os.system non blocking
os popen python 3
python wait for subprocess to finish
python subprocess
python wait until os system finishes
python os.system output
python os system shell

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 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.system call. This usually takes ~30s or so to complete. However, When I try to run the program, it instantly gives the error I mention above.

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

EDIT: I have ran the cline output in the command line version of the BLAST program. Everything appears to be fine.


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.

Python: waiting for external launched process finish, , the process you are launching is likely detaching itself to run in the background in which case the subprocess. 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 Blast(type, protein_sequence, start, e


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)

How to kill a process by name in Python, for the command to complete and then return the return code. The Python manual says nothing about whether os.system("cmd") waits or not for a process to end: To quote the manual: Execute the command (a string) in a subshell. It looks like it does wait (same behaviour as Perl's system ).


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.

Chapter 19, os. system() does wait for its process to complete before returning. If you are seeing it not wait, the process you are launching is likely detaching itself to run in the background in which case the subprocess. Popen + wait example Dor gave won't help. When you call os.system(abc123) python will wait until that process has completed before continuing. You may want to consider using something that simply spawns a subprocess and continues like You may want to consider using something that simply spawns a subprocess and continues like


Getting realtime output using Python Subprocess, When you call os.system(abc123) python will wait until that process has completed before continuing. You may want to consider using  The problem is that os.system does not actually wait for the file to be copied, and gets to executing the next line. This causes issues to the system. This causes issues to the system. I could actually give some wait functions, through time.sleep() , but we have to copy files with sizes ranging from 500 MB to sometimes 20 GB, and the times taken are very different.


Python script does not continue after os.system command , This document is for an old version of Python that is no longer supported. Wait for command to complete, then return the returncode attribute. The timeout argument is passed to Popen.wait(). The child process will block if it generates enough output to a pipe to fill up the OS pipe buffer as the pipes are not being read  os.system() does wait for its process to complete before returning. If you are seeing it not wait, the process you are launching is likely detaching itself to run in the background in which case the subprocess.Popen + wait example Dor gave won't help. Side note: If all you want is subprocess.Popen + wait use subprocess.call:


17.5. subprocess — Subprocess management, subprocess wait() waits forever, but os.system returns. Python Forums on Bytes. that it spits out some stdout that I collect, but I don't know the exe's source code.​) I can't figure out Popen thinks the process hasn't finished. Python popen command. Wait until the command is finished. I have a script where I launch with popen a shell command. The problem is that the script doesn't wait until that popen command is finished and go continues right away.