Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)
How can we call an external command from inside a Python script?

1 Answer

0 votes
by (107k points)

For that case, you need to use the subprocess module in the standard library:

import subprocess

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

The benefit of the subprocess.run over os.system is that it is more manageable(you can get the stdout, stderr, the "real" status code, better error handling, etc...).

On Python 3.4 and earlier, use subprocess.call instead of .run:

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

 Interested in Python?  Register to the Python Certification course and get certified.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...