Redirecting stdio from a command in os.system() in Python
On a unix system, you can redirect stderr and stdout to /dev/null as part of the command itself. os.system(cmd + “> /dev/null 2>&1”)
On a unix system, you can redirect stderr and stdout to /dev/null as part of the command itself. os.system(cmd + “> /dev/null 2>&1”)
First of all, you are cutting out the middleman; subprocess.call by default avoids spawning a shell that examines your command, and directly spawns the requested process. This is important because, besides the efficiency side of the matter, you don’t have much control over the default shell behavior, and it actually typically works against you regarding … Read more
What gets returned is the return value of executing this command. What you see in while executing it directly is the output of the command in stdout. That 0 is returned means, there was no error in execution. Use popen etc for capturing the output . Some thing along this line: import subprocess as sub … Read more
If all you need is the stdout output, then take a look at subprocess.check_output(): import subprocess batcmd=”dir” result = subprocess.check_output(batcmd, shell=True) Because you were using os.system(), you’d have to set shell=True to get the same behaviour. You do want to heed the security concerns about passing untrusted arguments to your shell. If you need to … Read more