E-Learning

Learn Python – How To Call an External Command?

 
Many times is necessary that Python interacts with the computer’s operating system where it is running. For example, to have information about the hard drive partitions. For this to happen, you must create an external command, which invokes the information or program that you want, directly from the OS.

In this post, we will show a Python module that allows processes to be created. These processes call external programs outside the code written in Python. It also allows the output of this program to be seen on the screen, and be run as if in a normal shell. This is the subprocess module, which offers several options to run external programs. We will show here two of them:
 

Python – The call option ()

import subprocess
r = subprocess.call ( "echo Hello World" shell =true)

This option shows on the screen the information that the external program has generated, but not as an entry that can be manipulated later by our program in Python. The value that the call () function returns within the Python program is only the status of implementation of the external program.

In order to manipulate the data from this external program, we use the check_output () function. An example using this function in place of call () is shown below:

subprocess.check_output (['Java -Djava.library.path= "C:  Users  Administrator  Documents  NetBeansProjects 
 SDK_Java_ v1.0.0.2_Beta  SDK Java_v1.0.0.2 BETA  Lib" -jar C:UsersAdministratorDocumentsNetBeansProjects
Busca3-N_javadistBusca3-N_ja‌va.jar',arquivo])

return r

This example is calling a Java program, whose information will be handled later in Python, being provided by return.

 

Python – The Popen option ()

import  subprocess
proc = subprocess.popen ( "Echo Hello World" shell = True, stdout = subprocess.PIPE) .stdout.read ()

It offers great flexibility for developers to be able to handle more complex cases which are not covered by the simplest functions. The subprocess.Popen () runs a child program in a new process. At Unix, it has a similar behavior to os.execvp () – when running the external program. At Windows, it uses the CreateProcess () function.

These are some of the ways to call an external command in Python. Do you know a different way? Share with us in the comments area below!

If you want to look for more tips, search our videos about Python. Below are some examples:

Aerolith.org – (Part 1)

 

Working on backend API

You can also follow some of our broadcasters who program in Python, as below:

 writkas

 

 

jakeinmn

 

 

Another cool way to find out interesting things about Python is to access our project page!

 

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

Recent Posts

Blockchain in Elections: A Leap Toward Transparent Democracy

In 2024 we're witnessing a critical point in democratic technology: the integration of blockchain and…

3 weeks ago

Win Big with Our Amazon Fire Max 11 & AirPods Pro Giveaway!

We’re thrilled to announce an exciting opportunity for you to win not one but two…

2 months ago

Unleashing Potential: How Education Ecosystem Transforms Learning into Real-World Success

Acquiring practical skills is crucial for career advancement and personal growth. Education Ecosystem stands out…

4 months ago

The Role of Artificial Intelligence in Modern Software Development

Artificial Intelligence (AI) has been making significant strides in various industries, and the software development…

7 months ago

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

8 months ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

9 months ago