• 0

How to call an external command in Python: with os.system or with the subprocess-module!?


Question

hello dear experts on neowin, 

 


How to call an external command in Python: with os.system or with the subprocess-module!?

 

Imagine i am on Windows and type at the command prompt from within a Python script? can i do this with  - the subprocess module in the standard library:

 

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


The official Python-documentation recommends the subprocess module over the alternative os.system():


my question is: which method id more powerful has more advantages - what are the downsides? 

a big big advantage from os.system: it passes the command and arguments to our system's shell.

 

 os.system("some_command with args"):

 

 

This is very cool - because we can perform multiple commands at the same time;  in this manner and set up pipes and input/output redirection. 

 

The advantage of subprocess vs. system is told to be 

 

- more flexible : since we can get the stdout, stderr, the "real" status code, .
- the subprocess module provides more powerful facilities for spawning new processes and retrieving their results; 
- and lots of other things more... 

 

 

to point out one of the advantages of os.system: 

 

os.system("some_command with args") passes the command and arguments to our system's shell.  a very very cool features: we are able to perform multiple commands at the same time.  and we can set up pipes and input/output redirection. 

 

os.system("some_command < input_file | another_command > output_file")  

 

Question: which approach do  you use - how do you call an external command in Python?

 

love to hear from you 
 

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.