How to run external command in Python?

How to run external command in Python?

Hi,

I am writing an application in Python and there is requirement of calling the external command from the Python program. I spend some time but could not find good example of calling external command.

How to run external command in Python?

Thanks

View Answers

May 13, 2017 at 7:36 PM

Hi,

In Python you have to use the sub-process. The subprocess module is very useful and provides many functionality.

I allows the developer to write program which can spawn a new processes and connect with main process with input/output/error pipes.

You can to use the sub process to achieve this.

For example following code:

from subprocess import call
call(["ls", "-l"])

Above command calls the external ls -l Linux command. This way you can easily call external program /command from Python main program.

Thanks









Related Tutorials/Questions & Answers:
How to run external command in Python?
ModuleNotFoundError: No module named 'pythonC'
Advertisements
ModuleNotFoundError: No module named 'pythonx'
ModuleNotFoundError: No module named 'pythong'
ModuleNotFoundError: No module named 'python4'
ModuleNotFoundError: No module named 'python4'
ModuleNotFoundError: No module named 'python2'
ModuleNotFoundError: No module named 'python2'
ModuleNotFoundError: No module named 'pythonp'
ModuleNotFoundError: No module named 'pythonp'
ModuleNotFoundError: No module named 'pythonp'
'bower' is not recognized as an internal or external command
How to run PHP Script from the command line ?
ModuleNotFoundError: No module named 'pythonz-bd'
ModuleNotFoundError: No module named 'dephell-pythons'
javac is not ecognised as internal and external command - Java Beginners
ModuleNotFoundError: No module named 'run_shell_command'
ModuleNotFoundError: No module named 'run_shell_command'
ModuleNotFoundError: No module named 'tox-run-command'
ModuleNotFoundError: No module named 'tox-run-command'
run command for all files in directory bash
run command prompt from jsp program
Java code to run unix command - Java Beginners
Run PySpark script from command line
How to run a Java program in CMD
how to run this program?
how to run - Java Beginners
how to run rmi program
how to run applet - Applet
How to compile and run the JSP
ModuleNotFoundError: No module named 'external'
ModuleNotFoundError: No module named 'external'
how to run servlet
how to run java program
How to run? - Hibernate
How to run a servlet
How to compile and run Java program
how to run a vlc player in jsp
how to run a .exe file in jsp
how to run servlet - JSP-Servlet
how to run ejb3.0 with jboss4.0.2 - EJB
how to run ejb3.0 with jboss4.0.2 - EJB
How to run JSP program inEclipse?
How to find run time in SQL.
how to run jsp appication in eclipse
ModuleNotFoundError: No module named 'external-arrow'
ModuleNotFoundError: No module named 'jenkins-external'
ModuleNotFoundError: No module named 'jenkins-external'
ModuleNotFoundError: No module named 'pycahrm-external'
ModuleNotFoundError: No module named 'pycahrm-external'

Ads