Тёмный

Python 3 Programming Tutorial - Subprocess module 

sentdex
Подписаться 1,4 млн
Просмотров 148 тыс.
50% 1

This Python programming tutorial covers the subprocess module. The subprocess module allows users to communicate from their Python script to a terminal like bash or cmd.exe.
Sample code for this basics series: pythonprogramming.net/beginner...
Python 3 Programming tutorial Playlist: • Why Python 3? Python 2...
seaofbtc.com
sentdex.com
hkinsley.com
/ sentdex
Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6

Хобби

Опубликовано:

 

1 авг 2014

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 107   
@alexp6012
@alexp6012 7 лет назад
I have recently started with Python and have been reading and watching videos. This is by far the best and easiest to understand explanation and I now understand it. Thank you!
@meltedcheese1908
@meltedcheese1908 6 лет назад
I like how you showed how to do both versions, great video man!
@JV-jc7ci
@JV-jc7ci Год назад
Wow this saved my (brain) life! The best tutorial after countless hours of trying to understand subprocess. Even my paid online course couldn't have done a better job. Thank you so much! You have a new subscriber.
@rexmorgan72
@rexmorgan72 7 лет назад
How does one send multiple command to the shell? For example, if I wanted to sftp into a machine the cd to a folder grab a file then exit the sftp connection. Do you simply add a + or do you have to do multiple subprocess.call lines
@lalu225
@lalu225 7 лет назад
Very helpful indeed. I've used this info to create 2 simple executable scripts on my Linux VPS. These scripts use sudo chown to change the owner of the 'wp-content' folder to 'www-data' in order to be able to update Worpress from the dashboard. Then I use the second script to change back the owner to my user name. Thanks for all your efforts Harrison, I very much enjoy this series and the one on Pandas.
@devprem11
@devprem11 7 лет назад
hey thanks for the video! I am opening a .exe with Popen ,that .exe user input prompt will come ,how to give those input though Python script
@BluePieNinjaTV
@BluePieNinjaTV 9 лет назад
do I need to use urllib.parse to parse the and the and all that? or is parse an inbuilt function?
@tomas-333
@tomas-333 6 лет назад
Has a video on piping in Python been made? If so, can someone reply with the link? Didn't see it in the playlist.
@0xbitbybit
@0xbitbybit 5 лет назад
Can you use \s in the string thing to tie it together instead of actual spaces?
@stallin06
@stallin06 7 лет назад
How do we pass the variable on the arguments.. any specific syntax?
@TheGT0000
@TheGT0000 8 лет назад
Wow, congraturation, good video.
@jonathanbrooks4227
@jonathanbrooks4227 8 лет назад
Hi there, I'm not sure whether you're aware or not, but if you put: universal_newlines=True within subprocess.check_output(), or most of the other functions within subprocess, then instead of receiving a binary stream, it is interpreted with new lines, so that stdout will look just as pretty as in the shell.
@miguelsantana8435
@miguelsantana8435 5 лет назад
SICK
@RakibFiha
@RakibFiha 5 лет назад
you are the man.
@adamthomas6643
@adamthomas6643 5 лет назад
Absolute legend thank you!
@basurajs
@basurajs 7 лет назад
Hi, i have installed python3 in windows10 machine and running script from windows machine only. I want to connect to linux machine by using SSH and run few commands. is it possible by using python subprocess module. If possible then please let me know.
@asishsahoo7163
@asishsahoo7163 7 лет назад
There are some windows command saved in a excel sheet and need to run those command one by one using python script.How to do it plz suggest.
@suryameda6215
@suryameda6215 5 лет назад
Can we terminate a process that is running in terminal using python, just quit the process and not totally killing the command prompt itself?
@bigstevebaker
@bigstevebaker 9 лет назад
When trying telnet, ssh, ftp etc. I could output and even login to a host but ping would always return a blank flashing cursor. I wouldn't assume we would have to import any networking modules since we are using the shell and ping is native on my Ubuntu host or am I incorrect sir?
@TOn-fx2gr
@TOn-fx2gr 5 лет назад
But when i call the commande vim to open a file it doesnt open and when i do cd it work but then when i do pwd the path is always the same and ls too
@ripperx444
@ripperx444 8 лет назад
I always get confused when needing to pipe the output for like ls -ltr | grep "test"..
@jeffg8416
@jeffg8416 2 года назад
Great video. Gotta appreciate tutorials that show germane examples and explain in terms you can grasp.
@nikhilvaliveti5717
@nikhilvaliveti5717 7 лет назад
I am running the same code as of u in python Idle it is showing that argv[1] doesn't exist why is this happening?
@manuj371
@manuj371 8 лет назад
I want two commands to communicate with PIPE [ | ], Eg. ls | grep yarn, how to do it ?? #python3
@mylifemywish3896
@mylifemywish3896 6 лет назад
please tell me anyone how to display a particular process at current time in python 2.7, i'm searching for it a lot but i'm not getting anywhere please anyone please help me
@amirmostaghimi7943
@amirmostaghimi7943 5 лет назад
Awesome and super helpful.
@Build_the_Future
@Build_the_Future 3 года назад
Can you use this with powershell?
@skaterrod123
@skaterrod123 6 лет назад
in this video you look like you were having a bad day and was like, "shit, i have to make a video today.... god damnit" lol, maybe is just me tho, great videos BTW (y).
@armanavetisyan8747
@armanavetisyan8747 7 лет назад
How can I use login credentials??? I'm trying connect to linux from Windows... pexpect doesn't work for windows... So I can use only Subprocess.. Pls show me example how can I use login credentials.. Thanks..
@gamesday1695
@gamesday1695 2 года назад
Hi could you please do tutorial on key based authentication ssh ppk file
@techkanakesh6765
@techkanakesh6765 5 лет назад
Can i run like this import subprocess print('enter the code you want to encode') code = input() def encode(): subprocess.call('echo "input()" | base64',shell=true) print('do you want to encode +'code' + y/n') option = input() if(option == y): encode() else: sys.exit() #ill input any random name or something is this really works please help me sir
@ashketchum7768
@ashketchum7768 8 лет назад
If you already made a video-tutorial on PIPEs, please post a link here.
@kapil-pj5ld
@kapil-pj5ld 9 лет назад
all your videos are awesome while I want to know about the piping so please upload the video on it and also changing windows directory using popen
@chunchunmaru2760
@chunchunmaru2760 5 лет назад
On the subprocess.check_output wouldn't you just decode the output? output = subprocess.check_output('ls', shell=True) print(output.decode("utf-8")) This will turn it into a string that would print the characters as new lines, giving you clean output
@varunhome2
@varunhome2 8 лет назад
hi guys im trying to pass this windows command through the subprocess.call but its not working..... here is the cmd subprocess.call('wevtutil epl System C:\system0506.evtx',shell=True) its working correctly if i type this manually in the cmd window.... but not working through python....can anybody help...
@jayanthkumar4132
@jayanthkumar4132 9 лет назад
cd command is not working using subprocess
@mattwilliams1844
@mattwilliams1844 6 лет назад
subprocess.run(["mkdir", "/home/user/Documents/whatever"]) It has to be enclosed in the square brackets, with each command that is separated by a space enclosed in quotations and separated by commas. Kind of a pain in the ass compared to the old import OS way of doing it
@kylewood303
@kylewood303 6 лет назад
a little advice: never use shell=True unless you really have to call the command through shell, and this video does not talk about exception handling such as zombie command running forever
@silaseul3186
@silaseul3186 9 лет назад
i use the same systut file and use the same command like you do subprocess,call(etc). in a .py file however if I try to run it it always Returns 1, no matter what Kind of Input i use ?? Main Programm: import subprocess x=subprocess.call("test_subprocess.py VAR", shell=True) print(x) test_subprocess Programm: import sys def main(arg): sys.quit(arg) if len(sys.argv)>1: main(sys.argv[1]) Output is always 1 no matter how Var is defined any suggestions ?
@FreeER
@FreeER 9 лет назад
So, I played with this and determined that test_subprocess was not getting any arguments passed through. A quick Google search reveals this SO post: stackoverflow.com/questions/10490529/python-subprocess-popen-not-sending-all-arguments-to-shell Which reveals the culprit as shell=True (which is a potential security vulnerability if you have external content (user input, other programs) affecting your built command/arguments anyways). The solution is to call python to run the script instead of using the shell: And the solution to know where the executable is (without hardcoding it) is to use sys.executable, of course you'll quickly realize that you also need an absolute path to the python script so use os.path.abspath (or shutils.abspath): # Main Program from sys import executable from subprocess import call from os.path import abspath x = call((executable, abspath('test_subprocess.py'), '5')) print(x) # now you probably noticed that I'm using a tuple here, but you could concatenate everything into one string # I just find the tuple a little more intuitive. # you'll also notice that I made VAR into the string '5'. All arguments must be strings # for me sys.quit(errorCode) didn't exist so I used sys.exit(errorCode) # I found that non-numeric errorCodes are ignored and 1 is used instead # so you must convert the string argv[1] to an int before returning it in test_subprocess. # test_subprocess from sys import argv, exit def main(arg): exit(arg) if len(argv)>1: try: x = int(argv[1]) except ValueError as e: print(e) exit(666) # use some sentinel that won't be assumed as a valid result main(x) # only get here if argv[1] exists and can be converted to an int # OK so, that's a bit limiting to ONLY be able to pass integer return codes... # so a more advanced method is using Popen and communicate (maybe there's a 3rd party module to simplify this?) # Main Program passes argv[1] to test_subprocess.py and prints it's return value from sys import executable, argv import subprocess from os.path import abspath if len(argv) < 2: sys.exit(1) # error x = subprocess.Popen((executable, abspath('test_subprocess.py')), stdin=subprocess.PIPE, stdout=subprocess.PIPE) # str does not implement buffer, bytes does. so convert strs to bytes with utf-8 encoding data = bytes(argv[1], 'utf-8') result = x.communicate(data) # result[0] is stdout, result[1] is stderr but that isn't PIPEd so it's always None # of course, output is returned as bytes as well bytes_result = result[0] # since I know I'm getting back a string we decode it as utf-8 real_result = bytes_result.decode('utf-8') print('x:', real_result) # x.terminate() # this didn't seem to work so well when I had while True: in the test_subprocess.py (more testing required) # test_subprocess.py import sys # input() or sys.stdin.read() reads strings. # but we had to print BYTES. so use sys.stdin.buffer.read() # to read BYTES in. Since we know those bytes are utf-8 encoded strings # decode them as utf-8 x = sys.stdin.buffer.read().decode('utf-8') try: x = int(x) if x < 100: print('This is some output') # result stdout else: print('Invalid input!') # result stdout, could have been stderr. except Exception: if x == 'hi': print("'ello mate!") # result stdout else: print('someone screwed up!') # result stdout, should probably be stderr... # huh.... maybe now would be a good time to actually, well, WATCH the subprocess module video I came here to watch.... lol # well, that check_output function is kindof nice... LOL. how did I miss that when reading through the docs...
@skasifali2202
@skasifali2202 2 года назад
If you try to runas command elevation mode....it is asking for the password...how to send the password from python using subprocess?
@k0jithemaster927
@k0jithemaster927 7 лет назад
Love the tuts sentdex, but I was wondering how I could use the subprocess.call with an input so if I had this. channel = input("please put the correct channel") address = input("please put address") subprocess.call(" bla bla {channel} ect {address}") how would I include the channel and address in that specific location, but obviously without the bla bla as I just needed to put something in as a reference.
@ashleygodden1269
@ashleygodden1269 7 лет назад
In python 3.6 you could do it like this. channel = input("please put the correct channel") address = input("please put address") subprocess.call(f'bla bla {channel} ect {address}') I haven't tested this code so it may not work, but good luck anyways.
@Cygnus0lor
@Cygnus0lor 6 лет назад
How many hours did you stay awake for this one? :))) Good job man
@wolfly2622
@wolfly2622 7 лет назад
awesome, ThankU 'Enter the password for fu: \x00 ' # i try to enter the password p.stdin.write('password '.encode()) # and then nothing will happen :( iam using 3.6 -n3x
@Pterry23real
@Pterry23real 9 лет назад
The subprocess call will be shown in pycharm and sublime tex how it should be. Maybe the IDE you mentioned was an exception ;)
@akeemlouigarde4946
@akeemlouigarde4946 2 года назад
Im using python 3.7.0 and "Check this out!" is not printing. It only shows me 0.
@fredyharter6293
@fredyharter6293 9 лет назад
Hey Harrison! I am looking for a good example code using sys and subprocess modules to communicate between python and non-python programs. I don't know any other programming language, but I feel like these two modules can open the door to other languages so I want to dig into examples and see how other programming languages allow to do that. Where do good examples hide on the net? :)
@sentdex
@sentdex 9 лет назад
Cyril Attié Not sure, I don't see many people mixing and matching myself. I just happen to think it's super cool. You could learn another language, even as simple as bash, or command line scripting, and mesh with that.
@fredyharter6293
@fredyharter6293 9 лет назад
You're right, it's very cool. I just installed Ubuntu in order to familiarize with it before building my own supercomputer, would you have any recommended source to learn bash?
@amanbansal2851
@amanbansal2851 6 лет назад
didnt get what you told in this lecture kindly provide suitable reply such that its useful in learning this topic, please.
@jonyjohan8958
@jonyjohan8958 5 лет назад
This is some real magic
@saminchowdhury7995
@saminchowdhury7995 7 лет назад
Your cool Man..REALLY COOL! Maybe explore Aduino with Python someday in your video! Anyways Thanks man your awesome
@pedrobarbosa8360
@pedrobarbosa8360 7 лет назад
Hey sentdex, I'm sorry, if you can help me ... I'm doing Jarvis in python for hobbie and i need to use one external program with python 3.6 called eSpeaker. You can orient me how will that do? sorry for bad english
@cyberarcannix1229
@cyberarcannix1229 7 лет назад
Teu inglês não é ruim, tem alguns erros, mas da pra entender perfeitamente o que você quer dizer. Por curiosidade, esta criando o bot apenas por diversão ou tem algum projeto maior que pretende seguir após fazê-lo ?
@pedrobarbosa8360
@pedrobarbosa8360 7 лет назад
O bot é apenas por hobbie. Por enquanto não tenho nenhum outro projeto , provavelmente no futuro , poderei pensar em algo mais para fazer
@zbzb-ic1sr
@zbzb-ic1sr 6 лет назад
Please make a tutorial on pipes thanks!
@AhmedKMoustafa2
@AhmedKMoustafa2 6 лет назад
did he make a tutorial on pipes ??
@nictuniema1249
@nictuniema1249 6 лет назад
Omg check it dude.
@user-qn3sg6zm6m
@user-qn3sg6zm6m 6 лет назад
I don't understand pipes and i want a tutorial on it so much.... It's a good idea but I want sub in the video because my English is not perfect.... Thank you a lot
@Treedboxcom
@Treedboxcom 5 лет назад
systut.py file content: from sys import argv def main(arg): print(arg) main(argv[1]
@STEVENopticus
@STEVENopticus 8 лет назад
I got some problems when I try to do "cd ensmo"--> The system cannot found the specified path. Can someone help me please??
@RREDesigns
@RREDesigns 7 лет назад
Does that directory actually exist in your folder structure? You can't cd into a non existent folder. -_-
@festerdam4548
@festerdam4548 7 лет назад
Create a directory called ensmo or cd to a diferent directory. P.S. It's better to learn the really basic things first.
@ashleygodden1269
@ashleygodden1269 7 лет назад
Really dude? Like I don't want to be too harsh here mate, but you have to learn the basics of a computer and especially the command cd before you can learn programming. Also it says the path can't be found which should obviously mean that there is no directory by the name of 'ensmo'.
@andriamichaels1027
@andriamichaels1027 8 лет назад
....not sure I understand the difference between subprocess.call and subprocess.check_out still....
@salauddin3693
@salauddin3693 7 лет назад
what is difference between subprocess.call and subprocess.check_call?
@ashleygodden1269
@ashleygodden1269 7 лет назад
check_call allows you to store the output from subprocess to a variable. It's usefull if you want to manipulate the output or get certain bits of data from the output.
@anjishnu8643
@anjishnu8643 6 лет назад
'import' is not recognized
@pratikkamat8148
@pratikkamat8148 7 лет назад
can you put a link to all the python programs you have.. so that we could refer it after your tutorials. Please man!
@sentdex
@sentdex 7 лет назад
pythonprogramming.net
@pratikkamat8148
@pratikkamat8148 7 лет назад
thnx man :)
@FreeER
@FreeER 9 лет назад
You don't need replaces to fix the output @5:08(unless you meant using a regex to pull data out of the result which is different, you did say 'parse' but I'll comment anyways since it's some extra information for other people and info is _nearly_ always good right?). it's printed with the because check_output returns a byte array (which is what the b prefix means for a string literal, see www.python.org/dev/peps/pep-3112/). Simply decode output (as ascii, or utf-8 which ascii is a subset of). ex: output = b'Hello ' print(output) # gives b'hello ' in 3.x, the expected 'hello'+newline in 2.x print(output.decode('ascii')) # gives the desired 'hello'+newline in 3.x and 2.x print(output.decode('utf-8')) # also gives the desired 'hello'+newline in 3.x and 2.x @11:30 you mention you think 'r is for return and n for newline'. Actually 'r' is for carriage return (so, close) and 'n' is for 'line feed', which are old terms carried over from typewriters, a 'carriage return' would return the carriage (the part that held and moved the paper as you typed) back to the beginning of the line and a 'line feed' would advance ('feed') the paper one 'line' down. Thus you'd be at the start of a new line ready to continue typing.
@GonsalvoDeCordova
@GonsalvoDeCordova 10 лет назад
Around 10:00 when you are flailing a bit, I think you could have done \' to escape those single quotes. Of course your final workaround was a better idea.
@comicks3800
@comicks3800 4 года назад
i just found the most underrated channel on youtube.
@saifghassan
@saifghassan 5 лет назад
lol I found you here too, you look so younger!
@Cygnus0lor
@Cygnus0lor 6 лет назад
9:48 Backslashhhhh! Escappeee!!!
@isaacthani
@isaacthani 7 лет назад
Trying to run subprocess... See what I keep getting: subprocess.call('dir', shell=True) Traceback (most recent call last): File "", line 1, in subprocess.call('dir', shell=True) NameError: name 'subprocess' is not defined
@javierbosch1338
@javierbosch1338 7 лет назад
you need to import subprocess
@isaacthani
@isaacthani 7 лет назад
Thanks.Figured it out.
@eldinekyd6400
@eldinekyd6400 8 лет назад
+sentdex hey sentdex realy ! it's cool video i realy like ur videos , and i just want to learn more ! so i ask if u know a plcae where can i found some exercices so i can practice and creat big programmes , so if u got any reference or something plz help !!
@spikespaz
@spikespaz 8 лет назад
+eldine kyd With your grammar, your not learning anything but kindergarten.
@user-qn3sg6zm6m
@user-qn3sg6zm6m 6 лет назад
Please add sub... And don't make a mistakes on your videos because i will be understanding... Thank you a lot... Keep doing this
@abhishekanand3285
@abhishekanand3285 6 лет назад
i do not understand sir ...:
@alexcohen8826
@alexcohen8826 8 лет назад
the fix to where your using subprocess to call systut.py would probably be: subprocess.call("python systut.py \"check this out\" ", shell=true)
@schoolmonkey13
@schoolmonkey13 8 лет назад
+alex cohen if that works, it'd probably be simpler to say subprocess.call('python systut.py "check this out" ', shell=true). I think the problem is that python recognizes both ' and " as indicating a string, while windows only recognizes " EDIT: Just saw that that's actually what he did. I still maintain that it's the best way, over the escaped "s.
@mayorjk2347
@mayorjk2347 3 года назад
i liked this part so much (((: 10:05
@ayeyefookinw0tm859
@ayeyefookinw0tm859 5 лет назад
But os.system is easier to use tho Why use subproses .
@nuramon9999
@nuramon9999 5 лет назад
its deprecated
@ayeyefookinw0tm859
@ayeyefookinw0tm859 5 лет назад
Pascal Ehrlich which one Os or subprocess
@willlywillly
@willlywillly 8 лет назад
you are looking tired my friend ;)
@mattwilliams1844
@mattwilliams1844 6 лет назад
I was gonna say drunk lol
@kmachine5110
@kmachine5110 6 лет назад
Try watching at 2x speed, he will look normal. (Y)
@cristianscript5649
@cristianscript5649 6 лет назад
who cares? you gossip girl
@Jo-id9zm
@Jo-id9zm 5 лет назад
I am the 666th like P-) Also, thanks for the quality content, as per yoush.
@risoflydoesminecraft987
@risoflydoesminecraft987 9 лет назад
Tip: just type this into python instead of print: variablenamegoeshere then press enter
@sentdex
@sentdex 9 лет назад
Risofly Does Minecraft If you're in an interactive interpreter, sure. Otherwise, no.
@risoflydoesminecraft987
@risoflydoesminecraft987 9 лет назад
I mean for an interactive interpreter
@k0jithemaster927
@k0jithemaster927 7 лет назад
Love the tuts sentdex, but I was wondering how I could use the subprocess.call with an input so if I had this. channel = input("please put the correct channel") address = input("please put address") subprocess.call(" bla bla {channel} ect {address}") how would I include the channel and address in that specific location, but obviously without the bla bla as I just needed to put something in as a reference.
@WoahMusicWoah
@WoahMusicWoah 6 лет назад
excuse the language
@cz_czan
@cz_czan 3 года назад
The annoying newline characters aren't there for no reason. The "b" before the single quotation marks implies that the parameter given to the print() function is of type byes. Hence you can parse it to a string (bytes.decode('utf-8') or perhaps str() works) and thus print it in a visually appealing way
Далее
WE COOKED A SHRIMP KEBAB  #recipe #barbecue #food
00:21
Просмотров 303 тыс.
Frank Now VS Then Edit 🥵| #brawlstars #shorts
00:18
Просмотров 347 тыс.
Multithreading Code - Computerphile
15:54
Просмотров 380 тыс.
Python WiFi
11:16
Просмотров 1,6 млн
BASH scripting will change your life
14:18
Просмотров 1 млн
Python Call a System Command!
10:14
Просмотров 24 тыс.
You Should Learn to Program: Christian Genco at TEDxSMU
10:26