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

Ads









Related Tutorials/Questions & Answers:
How to run external command in Python?
How to run PHP Script from the command line ?
Advertisements
'bower' is not recognized as an internal or external command
ModuleNotFoundError: No module named 'pythonC'
ModuleNotFoundError: No module named 'pythonx'
ModuleNotFoundError: No module named 'pythong'
ModuleNotFoundError: No module named 'python2'
ModuleNotFoundError: No module named 'python2'
ModuleNotFoundError: No module named 'python4'
ModuleNotFoundError: No module named 'python4'
ModuleNotFoundError: No module named 'pythonp'
ModuleNotFoundError: No module named 'pythonp'
ModuleNotFoundError: No module named 'pythonp'
ModuleNotFoundError: No module named 'pythonz-bd'
ModuleNotFoundError: No module named 'dephell-pythons'
ModuleNotFoundError: No module named 'tox-run-command'
ModuleNotFoundError: No module named 'tox-run-command'
ModuleNotFoundError: No module named 'run_shell_command'
ModuleNotFoundError: No module named 'run_shell_command'
run command for all files in directory bash
run command prompt from jsp program
Run PySpark script from command line
How to run a Java program in CMD
how to run rmi program
how to run this program?
How to compile and run the JSP
how to run java program
how to run a vlc player in jsp
how to run a .exe file in jsp
How to compile and run Java program
How to run JSP program inEclipse?

Ads