System calls using python

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.

Table of contents

  • Quick Examples to Execute a Program
  • Quick Examples to call a System command
  • 1. Using os.system() function to call a system command
  • 2. Using subprocess.run() function to call a system command
  • 3. Using subprocess.popen() function to call a system command
  • 4. Using the pexpect module to call a system command
  • 5. Using the asyncio module to call a system command asynchronously
  • 6. Using subprocess.run() function to execute program
  • 7. Using os.system() function to execute a program
  • 8. Using subprocess.popen() function to execute a program
  • 9. Using the pexpect module to execute a program
  • 10. Using the asyncio module to execute a program asynchronously
  • 11. The fastest way to execute program or call system command in python
  • 12. Summary and Conclusion
  • Related Articles
Читайте также:  Multithreading in java books

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 No 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: ") print(f"arg_b: ") # output : # arg_a: Hello # arg_b: world 

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.

1. 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.

Читайте также:  Docker add php ini

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"  ") # code in program.py file import sys arg_a = sys.argv[1] arg_b = sys.argv[2] print(f"arg_a: ") print(f"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(): seconds") print(f"elapsed time for subprocess.run(): seconds") print(f"elapsed time for subprocess.Popen(): 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 

12. 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.

You may also like reading:

Источник

Оцените статью