Running Shell Commands using Python (Detailed Explanation)

preview_player
Показать описание
In this video, learn how to run shell commands using Python. This is useful when your python program has to interact with another program via its CLI. All the concepts have been explained in detail.

------------------------------------------------------------

More awesome topics covered here:

------------------------------------------------------------

#python #shell #command
Рекомендации по теме
Комментарии
Автор

2:32 Old methods of running shell commands (os.system, os.popen, os.spawn)
7:25 Introduction to subprocess module
11:05 Run command and read output
15:43 Run command by passing single string (shell parameter)
16:40 Security considerations while using shell paramter
22:58 Run command and pass input
26:50 Run command with timeout
27:56 Run command and throw error if fail

IndianPythonista
Автор

Nice video! Quick note, shell is '/bin/sh', not '/usr/sh' :-)

makingachanneltopost
Автор

perfect vid! at the very end of the vid, you showed how to check if there is an error. I'm wondering how to not only check for error but also what specifically the error is about. How would you recommend doing it?

jackeyzhu
Автор

Nice useful video - thanks for making this available

grumpy_techo
Автор

how do I edit files(files like cat/etc/hosts or etc/selinux/config (the things that we can edit using vim or nano on terminal)) by using python(like i run the .py file in terminal and that file gets edited instead of me manually editing it) pls help if someone has any clue, or pls give me videos/ links related to this if anyone knows. need help

amandubey
Автор

#p = subprocess.run(["python3", "universal.py"], shell=True, capture_output=True, input='SP600-6')
#p = subprocess.run(["python3", "universal.py"], capture_output=True, input="SP600-6".encode(), text=True)
p = subprocess.run(["python3", "universal.py"], capture_output=True, stdin=open("a.txt", 'r'))
None of the above work? What am I doing wrong on windows machine?

vivekdabholkar
Автор

Hey Bro what if the shell command I am executing does need another input in the middle of processing. Like it needs to press Y/N to confirm the execution.
How can i do this.

fstreamdl
Автор

I have echo $1 in my shell script...I am trying to pass Hello as parameter/ input from python script.Any idea

prashanabalaji
Автор

Hi, I am getting error in using this subprocess to open the hostapd file in terminal of rasbian software. I am getting the error " Too many errors from stdin "

amangoyal
Автор

You are an amazing person. Thats what I was looking for. Thank you Sir!

kapilsonyt
Автор

Bro faceing issue
subprocess' has no attribute 'run
how to fix it

axosolaman
Автор

is there any way to run multiple shell commands in the same process instance? like reusing Popen?

utkarshshrivastava
Автор

Thanks for this video love you brother

axosolaman
Автор

os kya hei bro? And what is the theme ?

hirakhax
Автор

Hi this video was so interesting. I have stuck in one of the issue while using this run with minicom.
problem : running this command ["minicom", "-D", "/dev/ttyUSB1"] after this i will turn on device that will start booting for 30 secs and it will ask for inputs. how to do that?
is running commands once minicom triggered is it possible?

srinivaskalwad
Автор

How to awk command in python, I tried using it with subprocess but getting below error .

awk is not recognised as an internal or external command .

yeswanthkumar
Автор

My doubt is suppose I have a bat/shell script file that accepts multiple user inputs... So how can we execute this bat file from the python script and even handle/automate the user inputs from python script itself?

DhimanGhoshYT
Автор

Very, very, Very good!
Thank you Very much!

balbinodejesus
Автор

Wow that was so helpful!! I didn’t realize how straightforward it could be with the subprocess module.

I was so overwhelmed I tried to just write a bash script instead to run shell commands but that ended up taking forever because my bash skills aren’t that good yet.

Thanks and Keep up the great work!

futuregootecks
Автор

Hi Nikhil, your tutorials are very informative. Thanks for uploading!

anonnewuserp