How to call an external command

0 votes
How do you call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?
Nov 19, 2020 in Python by anonymous
• 8,910 points
356 views

1 answer to this question.

0 votes

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 stdout, stderr, 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()].

answered Nov 19, 2020 by Gitika
• 65,910 points

Related Questions In Python

0 votes
1 answer

How to call an external command from Python?

Hi@akhtar, You can use the os module in ...READ MORE

answered Jun 25, 2020 in Python by MD
• 95,440 points
498 views
0 votes
1 answer

How to call an external command?

Hello @kartik, Look at the subprocess module in the standard ...READ MORE

answered Nov 18, 2020 in Python by Niroj
• 82,880 points
321 views
0 votes
1 answer

How to call Python script from bash with an agrument?

Hi, You need to call the same command ...READ MORE

answered Jun 25, 2019 in Python by Shabnam
• 930 points
11,519 views
0 votes
1 answer

How to call function that takes an argument in a Django template?

Hello @kartik, You cannot call a function that ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
12,387 views
0 votes
0 answers

How do I execute a program or call a system command?

How do I call an external command ...READ MORE

Apr 24, 2022 in Python by Kichu
• 19,050 points
216 views
0 votes
1 answer

Restart python script automatically even when it crashes in Linux

Maybe this would be more robust? 1) save ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,090 points
2,760 views
0 votes
1 answer

Restart python script automatically even when it crashes in Linux

Maybe this would be more robust? 1) save ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,090 points
3,693 views
0 votes
1 answer

External command in Python

you can check the subprocess module in ...READ MORE

answered Oct 1, 2018 in Python by SDeb
• 13,300 points
422 views
+1 vote
3 answers

How can I use python to execute a curl command?

For sake of simplicity, maybe you should ...READ MORE

answered Oct 11, 2018 in Python by charlie_brown
• 7,720 points
93,203 views
0 votes
3 answers

How to distinguish between a variable and an identifier?

An identifier actually identifies something, it's not the thing ...READ MORE

answered Dec 16, 2020 in Python by Rajiv
• 8,910 points
60,351 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP