-->

Welcome to our Coding with python Page!!! hier you find various code with PHP, Python, AI, Cyber, etc ... Electricity, Energy, Nuclear Power

Monday 1 June 2020

Calling an external command from Python

All about Agile, Ansible, DevOps, Docker, EXIN, Git, ICT, Jenkins, Kubernetes, Puppet, Selenium, Python, etc

Calling an external command from #Python


Look at the subprocess module in the standard library:
import subprocess
subprocess.run(["ls", "-l"])
The advantage of subprocess vs. system is that it is more flexible (you can get the stdoutstderr, the "real" status code, better error handling, etc...).
The official documentation recommends the subprocess module over the alternative os.system():
The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function [os.system()].
The Replacing Older Functions with the subprocess Module section in the subprocess documentation may have some helpful recipes.
For versions of Python before 3.5, use call:
import subprocess
subprocess.call(["ls", "-l"])

No comments:

Post a Comment

Thanks for your comments

Rank

seo