Complete the code to start a new process in Python using the subprocess module.
import subprocess subprocess.[1](['echo', 'Hello World'])
The subprocess.run() function is used to run a command in a new process and wait for it to complete.
Complete the code to create a new process that runs a Python script asynchronously.
import subprocess process = subprocess.[1](['python3', 'script.py'])
subprocess.Popen() starts a process asynchronously, allowing the program to continue without waiting.
Fix the error in the code that tries to terminate a running process.
import subprocess proc = subprocess.Popen(['sleep', '10']) proc.[1]()
The terminate() method sends a signal to stop the process gracefully.
Fill both blanks to check if a process has finished and get its exit code.
import subprocess proc = subprocess.Popen(['ls']) if proc.[1]() is not None: exit_code = proc.[2]
poll() checks if the process has finished without waiting, returning None if still running.
returncode gives the exit status of the process after it finishes.
Fill all three blanks to start a process, wait for it to finish, and capture its output.
import subprocess result = subprocess.[1](['echo', 'Hello'], capture_output=[2], text=[3]) print(result.stdout)
subprocess.run() runs the command and waits for it to finish.
Setting capture_output=True captures the output.
Setting text=True returns output as a string instead of bytes.