with open ("log.txt",'a') as log:
output = subprocess.Popen("ech ;echo hello", shell=True, stdout=log, stderr=subprocess.STDOUT)
print output.wait() # prints 0
cd .. ; pwd
You need to do what I always recommend people do as the very first lines of any Bash script:
set -e (quite standard) set -o pipefail (requires Bash 3 or later)
Then, whenever a command (or part of a pipeline) fails, the whole script will fail. This will cure your problem by stopping as soon as
ech is not found.