import subprocess # Run the command "ls -l" and capture its output output = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE).communicate()[0] # Print the output print(output)
import subprocess # Run the command "grep hello" and send input to it p = subprocess.Popen(['grep', 'hello'], stdin=subprocess.PIPE) # Send some input to the process p.communicate(b"hello world\nhi there\n") # The process will exit after receiving the input
import subprocess # Run the command "echo hello" and check its return code p = subprocess.Popen(['echo', 'hello']) return_code = p.wait() # Print the return code print(return_code)This example runs the `echo hello` command (which simply prints the word 'hello') using `Popen`, waits for the process to exit using the `wait` method, and prints the return code. The `subprocess` module is part of the Python standard library.