Advantages of subprocess over os.system

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

How to get the output from os.system()? [duplicate]

Use subprocess: import subprocess print(subprocess.check_output([‘nslookup’, ‘google.com’])) If the return code is not zero it will raise a CalledProcessError exception: try: print(subprocess.check_output([‘nslookup’, ‘google.com’])) except subprocess.CalledProcessError as err: print(err) os.system only returns the exit code of the command. Here 0 means success. Any other number stands for an operating-system-dependent error. The output goes to stdout of this … Read more

Fetching the output of a command executed through os.system() command [duplicate]

Use the subprocess module: subprocess.check_output returns the output of a command as a string. >>> import subprocess >>> print subprocess.check_output.__doc__ Run command with arguments and return its output as a byte string. If the exit code was non-zero it raises a CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute and … Read more

Python: How to get stdout after running os.system? [duplicate]

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