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

1 Answer

0 votes
by (108k points)

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

import subprocess["ls", "-l"])

The benefit of the 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 instead of .run:["ls", "-l"])

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

Browse Categories