by Sai gowtham

Calling an external command from Python

This example will show you how to call an external command from Unix shell or Windows command prompt in a python script.

We need to use the subprocess module in python.

import subprocess
subprocess.run(["ls", "-l"])

For old versions, we need to use the call method.

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

The subprocess module allows us to spawn new processes and connect to their input/output/error pipes, and obtain their return codes.

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
130,921 students enrolled
48.5 hours of video content
View Course