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,870 points
589 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,770 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,460 points
916 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,840 points
525 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,926 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,840 points
13,027 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,040 points
443 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,020 points
3,099 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,020 points
4,046 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
689 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
94,490 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,870 points
65,313 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