Python windows call program

Running external programs with Python (os.system and subprocess)

One of the things we often have to do is glue together various programs written by other people.
If these other programs are GUI based then we will have a very hard time doing so, but if they are command line based then there are some nice ways to do that in Python. We’ll see two different ways to accomplish this.

Our external program

In order to demonstrate this we need an «external tool» that we will handle as a «black box». As you are using Python I can assume you already have Python on your computer so we’ll use a script written in Python as the «external tool». You can see it here:

import time import sys if len(sys.argv) != 3: exit(f"sys.argv[0]> SECONDS EXIT_CODE") seconds = int(sys.argv[1]) exit_code = int(sys.argv[2]) for sec in range(seconds): print("OUT <>".format(sec), flush=True) print("ERR <>".format(sec), file=sys.stderr) time.sleep(1) exit(exit_code) 
  • Output on Standard Output (STDOUT)
  • Output on Standard Error (STDERR)
  • A process that takes time
  • Various exit codes (ERRORLEVELs)

So we can see how to deal with either of those.

The user of the process.py can tell it how many iterations to do. On every iteration it will print to both STDOUT and STDERR and wait for 1 second.
The user can also tell the process how to set its exit code.

This can be a nice tool to fake the behavior of some external tool.

If we run the process as follows:

We get the following output:

OUT 0 ERR 0 OUT 1 ERR 1 OUT 2 ERR 2 

We can also observe the exit code on Linux/macOS:

Using os.system

The simplest way to run an external program is to use os.system .

import os import sys exit_code = os.system(f"python process.py 5 2") print(f'exit code: exit_code // 256>') 

It accepts a string — exactly what you would type in on the command line and executes the external program.

Your program connects its own STDOUT and STDERR channels to the external process so while the external program is running
whatever it prints to STDOUT and STDERR will be handled exactly as if they came directly from your program.

It waits till the external program ends and at the end it returns the exit-code of the external program.
Well it actually returns two bytes and the real exit code is in the higher byte so we need to do an integer division
exit_code // 256 in order to get to the real value. (It is the same as int(exit_code / 256) .)

OUT 0 ERR 0 OUT 1 ERR 1 OUT 2 ERR 2 OUT 3 ERR 3 OUT 4 ERR 4 exit code: 2 

This can be very useful, but this way the output of the external program «gets lost» to our program. Often this is not what we want.

Often we would want to capture the output of the external program, parse it and do something based on the understanding from it.

We might also want to do something else while the external program does its thing.

Let’s see how the subprocess module can help us. We will see a few examples.

subprocess waiting for external process to finish

In the first example we will imitate the os.system just to lay the ground-work.

We have created a function called run_process . Instead of a string, the command we would want to type in,
it is expected to receive a list. The pieces of the command divided up. That is probably not be a problem to write.

I sprinkled the whole program with print statements to make it easier to see what is the order of things happening.

The first thing is to call proc = subprocess.Popen(command) . This will start the external program and return immediately
passing us an object that represents this external process. ( Popen stands for process open )

At this point the external program will run regardless of what our program does. So we can wait for 1.5 seconds and see the output (and error)
of the external program. We could also do some other work while the external program runs. We’ll see that later.

At one point, however, we will likely want to wait for the external program to end. This is what the proc.communicate() does.
(It’s name is strange, I know. The next example will shed some light on why it is called that way.)
It stops our program and waits till the external program ends.

Then we can fetch the exit code (that Windows calls ERRORLEVEL) from the attribute returncode of the proc object.

(Are you already having fun by the fact that the same thing is called «exit code», ERRORLEVEL, and «returncode» by three different systems?)

import subprocess import time def run_process(command): print("Before Popen") proc = subprocess.Popen(command) # This starts runing the external process print("After Popen") time.sleep(1.5) print("Before communicate") proc.communicate() print("After communicate") exit_code = proc.returncode return exit_code print("Before run_process", flush=True) exit_code = run_process(['python', 'process.py', '5', '0']) print("After run_process", flush=True) print(f'exit code: exit_code>', flush=True) 

Источник

Python Run Shell Command On Windows

Welcome to Python Run Shell Command On Windows tutorial. In this tutorial, you will learn, how to run shell command in python. So let’s move forward.

Python Run Shell Command On Windows

What is Shell ?

  • In computer science shell is generally seen as a piece of software that provides an interface for a user to some other software or the operating system.
  • So the shell can be an interface between the operating system and the services of the kernel of this operating system

Python Modules For Running Shell command

Python provides lots of modules for executing different operations related to operating system.

Generally there are two important modules which are used to run shell command in python.

Python Run Shell Command Using Subprocess module

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions:

The subprocess module allows users to communicate from their Python script to a terminal like bash or cmd.exe.

Now we will see different functions of subprocess module.

subprocess.call()

call() method create a separate process and run provided command in this process.

Write the following code to implement call() method of subprocess module.

Источник

Читайте также:  Числа фибоначчи python поиск
Оцените статью