import subprocess def run_command(command): process = subprocess.Popen(command, shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE) output, error = process.communicate() return output.decode(), error.decode() command = "psql lts_ets_pro -f statement3.spl > event_result.txt" ''' output, error = run_command(command) print(output) if error: print(error)'''