You are currently viewing Python Execute Program or Call a System Command

How to execute a program or call a system command in Python? In this article, we will discuss how to execute a program or call a system command from within a Python script. We will explore different approaches to executing external programs and calling system commands, including using the subprocess module, the os module, and the Popen class.

Advertisements

1. Quick Examples to Execute a Program

Learn how to execute a program in Python with these quick examples. We will provide a complete list of possible approaches. However for now you will have a high-level overview of how can you do it.


# Example 1 : Execute a Python Program
import subprocess
# program.py is another python file
path_to_program = 'program.py'
# Execute a program using subprocess.run() 
result = subprocess.run([path_to_program],shell=True)
# output : I am a Program from another file

# Example 2 : Execute Program with Args
# code in app.py
import subprocess
arg_a = 'Hello'
arg_b = 'world'
# Execute a program using subprocess.run() 
subprocess.run([ 'program.py', arg_a, arg_b],shell=True)

# code in program.py
import sys
arg_a = sys.argv[1]
arg_b = sys.argv[2]
print(f"arg_a: {arg_a}")
print(f"arg_b: {arg_b}")

# output : 
# arg_a: Hello
# arg_b: world

2. Quick Examples to Call a System Command

In the same way, we can also use Python to call system commands as well. The below examples can be run on any windows operating system. You can change the commands and see the result accordingly.


import subprocess

# Example 1: Delete a filue using the 'del' command
result = subprocess.run(["del", 'index.html'], shell=True)
print(result)

# Example 2: Get a list of current tasks
result = subprocess.run(["tasklist"], shell=True)
print(result)

In the above example, we have called the tasklist command. The tasklist system command will display a list of currently running tasks.

3. Using os.system() Function to Call a System Command

To execute system commands from within a Python script, the os.system() function can be used. The os.system() function returns the exit status of the command as an integer. A return value of 0 indicates success, while a non-zero value indicates an error.


import os

# Execute the "mkdir" command to create a new Directory
exit_status = os.system("mkdir 'New Directory'")

# Check the return value
if exit_status == 0:
    print("Command succeeded")
else:
    print("Command failed")

2. Using subprocess.run() Function to Call a System Command

You can use subprocess.run() in the same way we use the os.system() function to call a system call. Just make sure that the command is available on your system. Otherwise, it will just ignore the command and give you no errors.

To call a system command using the subprocess.run() function, you simply pass a list containing the command and any arguments as the first argument to the function.

I have code in my System command when I run it open VScode. I will now open VSCode using the Python subprocess.run() function.


# Example 1:
import subprocess

# Open VScode using subprocess
subprocess.run(['code'], shell=True)

# Example 2:
import subprocess

# Create a new Directory using subprocess
dir_name='Python Programs'
subprocess.run(['mkdir', dir_name], shell=True)

3. Using subprocess.popen() Function to Call a System Command

The subprocess.Popen() function allows you to create a new process and communicate with it using pipes. To call a system command using the subprocess.Popen() function, you pass a list containing the command and any arguments as the first argument to the function.


import subprocess

# Call the "tasklist" command and capture the output
process = subprocess.Popen(["tasklist"], stdout=subprocess.PIPE)
output, _ = process.communicate()

# Print the captured output
print(output)

4. Using the pexpect module to Call a System Command

pexpect allows you to call a system command and interact with its output in a way that is similar to a human user.It can also be used for interacting with child processes.

pexpect module, is a third-party library so you need to install it using pip. Once you install it you will be able to call the system command within your python code.

The following code will only work on a Unix environment (Linux, Mac, or I think WSL should work).


import pexpect

# Spawn a child process
child = pexpect.spawn("mkdir mydir")

5. Using the asyncio module to call a System Command Asynchronously

The asyncio module provides a number of utilities for working with asynchronous tasks, including the ability to call system commands asynchronously. you can call a system command in a new process and wait for it to complete asynchronously.


import asyncio

async def call_tasklist():
    # Create a subprocess and call the "tasklist" command
    process = await asyncio.create_subprocess_exec("tasklist")

    # Wait for the process to complete
    await process.wait()

# Run the async function
asyncio.run(call_tasklist())

The create_subprocess_exec() function returns a subprocess.Process object that represents the new process. The wait() method of this object can be used to wait for the process to complete.

6. Using subprocess.run() Function to Execute Program

In the same way, you run the system call, you can also execute a program using the subprocess.run() function. To execute a program using the subprocess.run() function, you simply pass a list containing the program name and any arguments as the first argument to the function.


import subprocess

# Execute the "python" program 
subprocess.run(["python", "-c", "print('Hello, world!')"])

7. Using os.system() Function to Execute a Program

To execute a program using the os.system() function, you simply pass the full path to the executable file as a string argument. In the previous example, we have seen how to call system commands using the os.system() function. Now here is an example of executing a python program using the os.system() function.


import os

# Execute a Python program
os.system("program.py")

We can also pass arguments required by the program that we are calling. We can pass arguments to the program by including them in the string separated by whitespace. Check the following example.


import os

# Execute a Python program with arguments
path='program.py'
arg_a='Hello'
arg_b='World'
os.system(f"{path} {arg_a} {arg_b}")

# code in program.py file
import sys
arg_a = sys.argv[1]
arg_b = sys.argv[2]
print(f"arg_a: {arg_a}")
print(f"arg_b: {arg_b}")

We have done the same job using the subprocess.run() function. We will look over the comparison over that later on but for now, you know that we can do it in many ways.

8. Using subprocess.popen() Function to Execute a Program

The subprocess.Popen() function takes a list containing the program name and any arguments as its first argument, and returns a Popen object representing the new process.


import subprocess

# Execute the "echo" command 
process = subprocess.Popen(["echo", "Hello, world!"],shell=True, stdout=subprocess.PIPE)
# Read the stdout stream
output = process.stdout.read()
# Print the output
print(output)
# Wait for the process to complete
process.wait()

9. Using the pexpect module to Execute a Program

Run the code in a Unix environment (Linux, Mac, or I think WSL should work). One of the primary functions is the pexpect.spawn() method, which can be used to create a new child process and execute a program.


import pexpect

# Execute the "python" program
child = pexpect.spawn("python")

# Send a command to the program and wait for the output
child.sendline("print(\"Hello, world!\")")
child.expect("Hello, world!")

# Send another command and wait for the output
child.sendline("print(2 + 2)")
child.expect("4")

# Close the child process
child.close()

10. Using the asyncio module to Execute a Program Asynchronously

This can be useful for tasks that may take a long time to complete, such as executing a program. In this example, the execute_program() function is defined as an asyncronous function using the async keyword. The create_subprocess_exec() function is used to create a subprocess to execute the myprogram program.


import asyncio

async def execute_program():
    process = await asyncio.create_subprocess_exec("myprogram.exe")
    await process.wait()

asyncio.run(execute_program())

11. The fastest way to execute program or call system command in Python

We will draw a comparison between the most commonly used methods for calling system commands in python. We will see which one is the fastest method based on performance in the following particular case. This conclusion is drawn for calling system command but you can use it for executing program as well.


import timeit
import os
import subprocess

# Define the number of iterations to execute the command
num_iterations = 1000

# Define the command to execute
command = "echo hello"

# Measure the time it takes to execute the command using os.system()
elapsed_time_os_system = timeit.timeit(
    stmt="os.system(command)",
    setup="import os; command = 'echo hello'",
    number=num_iterations
)

# Measure the time it takes to execute the command using subprocess.run()
elapsed_time_subprocess_run = timeit.timeit(
    stmt="subprocess.run(command, shell=True)",
    setup="import subprocess; command = 'echo hello'",
    number=num_iterations
)

# Measure the time it takes to execute the command using subprocess.Popen()
elapsed_time_subprocess_popen = timeit.timeit(
    stmt="""
process = subprocess.Popen(command, shell=True)
process.wait()
""",
    setup="import subprocess; command = 'echo hello'",
    number=num_iterations
)

# Print the elapsed times
print(f"elapsed time for os.system(): {elapsed_time_os_system:.6f} seconds")
print(f"elapsed time for subprocess.run(): {elapsed_time_subprocess_run:.6f} seconds")
print(f"elapsed time for subprocess.Popen(): {elapsed_time_subprocess_popen:.6f} seconds")

On my system, the performance of the 3 methods is very much closed. Check the below output of the code. You can check it on your computer as well.


elapsed time for os.system(): 26.422583 seconds
elapsed time for subprocess.run(): 27.169089 seconds
elapsed time for subprocess.Popen(): 28.721695 seconds

Frequently Asked Questions

How can I execute a system command in Python?

You can execute a system command in Python using the os.system() function or the more versatile subprocess module.

What is the difference between os.system() and subprocess?

os.system() is a simple way to execute commands but has security risks due to shell injection. subprocess is more powerful, providing better control over input/output and avoiding shell injection vulnerabilities.

How do I pass arguments to a system command in Python?

You can pass arguments to a system command by including them as part of the command string.

How can I capture the output of a system command?

To capture the output of a system command, use the subprocess module. You can use subprocess.check_output() or subprocess.run() with capture_output=True

What is shell injection, and how can I prevent it?

Shell injection is a security vulnerability where untrusted input is passed to a shell command, allowing an attacker to execute arbitrary commands. To prevent shell injection, avoid using os.system() with untrusted input. Instead, use the subprocess module with a list of arguments.

Can I execute system commands in a platform-independent way?

The subprocess module allows you to execute system commands in a platform-independent way, making it suitable for cross-platform development.

Summary and Conclusion

we have learned about how to execute a Program or Call a System Command. We have also learned about os.system(), subprocess.run(), and subprocess.Popen() functions for calling system commands in Python. If you have any questions about using Python to call system commands, please leave a comment below.