#!/usr/bin/env python import subprocess # use sequence of arguments ret = subprocess.call(['echo', 'Hello world!']) # shell=True allows to use simple string for the command ret = subprocess.call('echo Hello world!', shell=True) print() command = 'date' print(command) # checks output, raises error if nonzero return code output = subprocess.check_output(command, shell=True) p = subprocess.Popen('date', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) out, err = p.communicate() returncode = p.returncode #return_code = p.wait() print(output.decode('utf-8')) print('out:', output.decode('utf-8')) print('err:', err.decode('utf-8')) print('status:', returncode) # write to stdout and logfile logfile = open('logfile', 'w') proc=subprocess.Popen(['cat', 'file'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) for line in proc.stdout: sys.stdout.write(line) logfile.write(line) proc.wait()