• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

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

Question

tarifa    0

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 
 

Share this post


Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.