Running shell cmds from python

This is done using python's subprocess module. To run a cmd and just get the return code returned, use subprocess.call(). Internally this function uses Popen(), so its arguments are the same as for the Popen constructor (see below). For example:
retval = subprocess.call(args = """find . -maxdepth 2 -empty -exec rm {} \;""", shell = True, stdout = None)
If you need to PIPE in/output data between caller- and sub-process, create a Popen() instance and call communicate() on it. For example, the equivalent of ps aux | grep my_daemon_name will be:
# execute cmd "ps aux"
p1 = Popen(["ps", "aux"], stdout=PIPE)
# execute cmd "grep my_daemon_name", with cmd's stdin set to stdout of previous "ps aux" cmd
p2 = Popen(["grep", "my_daemon_name"], stdin=p1.stdout, stdout=PIPE)
# retrieve the stdout data from grep
output = p2.communicate()[0]
See also PyMOTW for more examples.

class subprocess.Popen
class Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None,
preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None,
universal_newlines=False, startupinfo=None, creationflags=0)
Note that Popen() can execute a cmd in 2 possible ways, depending on the "shell" argument. The args argument may only be a string only if shell is set to True.
  • shell set to False (default) : In this case, the Popen class uses os.execvp() to execute the child program. args should normally be a sequence.
  • shell set to True : the command string is executed through the shell. args may be a string or a sequence (which is being converted into a string -> first item is used as the cmd, others are treated as additional shell arguments.
stdin, stdout, and stderr specify the subprocess's standard input, output, and error files, respectively. Each may be:
  • PIPE, which creates a new pipe to/from the subprocess;
  • None, meaning that the subprocess is to use the same file as this ("parent") process;
  • a file object (or file descriptor) that's already suitably open (for reading, for the standard input; for writing, for the standard output and standard error)
  • stderr may also be STDOUT, meaning that the subprocess's standard error is to occur on the same file as its standard output.
An instance p of class Popen has following methods and attributes:
  • pid : The process ID of the subprocess.
  • returncode : None to indicate that the subprocess has not yet exited; otherwise, an integer: (0 for success termination, >0 for error termination, -x if cmd failed to execute properly)
  • stderr, stdin, stdout : When the corresponding argument to Popen was subprocess.PIPE, each of these attributes is a file object wrapping the corresponding pipe; otherwise, each of these attributes is None.
  • communicate(input=None) : Sends the string input as the subprocess's standard input (when input is not None), then reads the subprocess's standard output and error files into in-memory strings until both files are finished and finally waits for the subprocess to terminate and returns a pair (two-item tuple).
  • poll() : Checks if the subprocess has terminated, then returns p.returncode.
  • wait() : Waits for the subprocess to terminate, then returns p.returncode.

Comments

Popular posts from this blog

Handling control characters (escaping) in python for json and mysql

python port sniffer with pcapy and impacket

Django field, form and model validation process