0

If I run this below I am shown the return straight away on screen

   #!/usr/bin/python3    
    import os    
    myVar = os.system('echo, Hello')
 Hello

How do I stop it being shown but save the result for use later? I expected to be able to print the results using print (("%s")% myVar) but that just returns 0

Anthony
  • 95
  • 2
  • 11

2 Answers2

1

You can use check_output from subprocess like this:

import subprocess
output = subprocess.check_output(['echo', 'Hello'], shell=True)
print(output)
Mikicat
  • 73
  • 2
  • 9
0

You could use subprocess module for this

import subprocess

yourProc = subprocess.Popen("echo, Hello", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout, stderr = yourProc.communicate()
print(stdout)