Python Tutorial: Calling External Commands Using the Subprocess Module

///Python Tutorial: Calling External Commands Using the Subprocess Module

Python Tutorial: Calling External Commands Using the Subprocess Module

FavoriteLoadingAdd to favorites

In this Python Programming Tutorial, we will be learning how to run external commands using the subprocess module from the standard library. We will learn how to run commands, capture the output, handle errors, and also how to pipe output into other commands. Let’s get started…

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey’s Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –

#Python

source

By |2019-10-19T23:28:39+00:00October 19th, 2019|Python Video Tutorials|39 Comments

39 Comments

  1. Corey Schafer October 19, 2019 at 11:28 pm - Reply

    Hey everyone. I hope you found this video useful. As I mentioned in a previous post, I just finished renovating a house that I'm selling and am currently going through a cross-country move, so I haven't been able to post as much lately as I'd like. Once I get this house sold and get settled into my new place then I'll be releasing at a much faster pace. Thanks for your patience… especially those who are contributing through Patreon or are Channel Members! I appreciate all of you! Hope you're all having a great week!

  2. Dennis Asamoah October 19, 2019 at 11:28 pm - Reply

    Great as usual

  3. abhay prajapati October 19, 2019 at 11:28 pm - Reply

    in p2 subprocess, input is given as a string(since text=true), but if i try to give a string as input, it shows that it needs a byte like object as input but not a string.

  4. M. October 19, 2019 at 11:28 pm - Reply

    Great tutorial as always. Finally I can say goodbye to the hot mess that is Bash Scripting *shudders*

  5. g h October 19, 2019 at 11:28 pm - Reply

    why are some methods like DEVNULL written in all caps?

  6. abhay prajapati October 19, 2019 at 11:28 pm - Reply

    just got what i was looking for.! thanks @coryschafer

  7. Abhishek Kumar October 19, 2019 at 11:28 pm - Reply

    How can I run below command in CMD prompt using python:

    pactester.exe -p proxy.pac -u https://www.google.com -c 10.6.6.6
    Any suggestions??

  8. jay l October 19, 2019 at 11:28 pm - Reply

    Could not get any of those modifiers in subprocess to work. .run would not work, had to use Popen, Anything past that would not run

  9. Roger Wellard October 19, 2019 at 11:28 pm - Reply

    Easily some of the best python content on you tube. Many thanks

  10. sshah93 October 19, 2019 at 11:28 pm - Reply

    Hi Corey, I am trying to write a python script to convert a bunch of videos I have using FFMPEG. FFMPEG is executed via the command line, and after seeing your video, I am now using the Subprocess module to help me.

    Unfortunately, after setting `capture_ouput = True` on `sp.run()`, using `stdout` , how can I get the `stdout` command to print the constant command line changes, because when I run the ffmpeg command the script is executing the convert command in the command line, but `stdout` is not printing anything which i suspect is because it is waiting for the `sp.run()` command to finish executing.

    So can I capture a stream of outputs from the command line when executing a `sp.run()` command? if yes, how?

  11. Superdooperhero October 19, 2019 at 11:28 pm - Reply

    For running UNIX commands on Windows you can use GIT Bash.

  12. Yajant Bhawsar October 19, 2019 at 11:28 pm - Reply

    Hello Corey, could you please make a video on,
    "Interesting Google calendar" in python Django?
    Their are not much resources or tutorials available for it. We will be so glad to have one from you.

  13. Fernando Mendes Coimbra de Mendonça October 19, 2019 at 11:28 pm - Reply

    Oh man thank you for this video!! Saved my day!!

  14. Nagarjuna Maddi October 19, 2019 at 11:28 pm - Reply

    I am using Anaconda -> Spyder where I tried to use argument Capture_output in run but it throws error "TypeError: __init__() got an unexpected keyword argument 'check_output'" under subprocess

  15. Abim Joshi October 19, 2019 at 11:28 pm - Reply

    Help me with this!!
    Getting some error while trying out the given code:

    import subprocess
    p1 = subprocess.run(['ls', '-la'], stdout=subprocess.PIPE, text=True)
    print(p1.stdout)
    Error:
    Traceback (most recent call last):
    File "1.py", line 2, in <module>
    p1 = subprocess.run(['ls', '-la'], stdout=subprocess.PIPE, text=True)
    File "/usr/lib/python3.5/subprocess.py", line 693, in run
    with Popen(*popenargs, **kwargs) as process:
    TypeError: __init__() got an unexpected keyword argument 'text'

    I am running the above code in Ubuntu Terminal. Version: Python 3.5.2

  16. rubebop October 19, 2019 at 11:28 pm - Reply

    In Spyder
    subprocess.run("dir", shell=True)
    returns:
    CompletedProcess(args='dir', returncode=0)
    Why is that?

  17. Antonio Montana October 19, 2019 at 11:28 pm - Reply

    your voice is encouraging me to watch your videos.

  18. O October 19, 2019 at 11:28 pm - Reply

    Again, thank you Corey!

  19. Ræfæēl Sæñtøs October 19, 2019 at 11:28 pm - Reply

    He speaks very fast, and for me that I ain't native, it's harder to listening him
    If speak == 'Fast':
    speak = 'Low and Calm'
    Hahahaha

  20. Edward Sileo October 19, 2019 at 11:28 pm - Reply

    Holy cow! Great tutorial.

  21. zain tv October 19, 2019 at 11:28 pm - Reply

    Very Helpful, Just keep it up Corey!

  22. Ziffixture October 19, 2019 at 11:28 pm - Reply

    What platform is this you're writing the code in?

  23. rahul gupta October 19, 2019 at 11:28 pm - Reply

    Amazing video…thanks a lot for sharing knowledge…can you please create a video on how to run python script on remote server via ssh

  24. TN WU October 19, 2019 at 11:28 pm - Reply

    Great content as always.

  25. rasterops1083 October 19, 2019 at 11:28 pm - Reply

    Nice work Corey. Thanks 🙂

  26. Liberty Rebel October 19, 2019 at 11:28 pm - Reply

    Hey guys , i made a program with python that have a form and i can input a specific form.
    I have put 2 buttons, one that save the form in a text file and another that prints the form directly with my printer, but I can't figure out how to use my system printer and print directly with the button in the program.

  27. Sooraj Ghosh October 19, 2019 at 11:28 pm - Reply

    Its a really helpful video with all the necessary knowledge about the subprocess module.
    It was a great and very informative video.
    Cant thank enough for this..!

  28. Sagar Jagnade October 19, 2019 at 11:28 pm - Reply

    That was really useful.

  29. Bruno Ferreira Gomes October 19, 2019 at 11:28 pm - Reply

    what is the difference bewteen subprocess.run() and os.system()?

  30. Jimmy Lin October 19, 2019 at 11:28 pm - Reply

    Hi Corey, thank you for your amazing tutorial! Your python basic tutorial really helped when I first started learning python.
    Just one small problem, subprocess.run(["ls", "-la"], capture_output=True) should be subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE) on Mac (python 3.6).

  31. Huy Dong October 19, 2019 at 11:28 pm - Reply

    I really appreciate and love your tutorials. Could you please make a book version on all of that contents that you produced? So that I can easily to reference for convenience. I will be the one who buy your book first.

  32. メキシコ人 October 19, 2019 at 11:28 pm - Reply

    You should make books for this material. I love physical books and I can take notes and use references. If you made these videos as books I'd buy them! 💵

  33. Swadhikar C October 19, 2019 at 11:28 pm - Reply

    Hi all,
    Please join my page to get daily updates and learn advanced programming concepts

    https://www.facebook.com/lovepythoncode/

  34. Aditya Prajapati October 19, 2019 at 11:28 pm - Reply

    Can you please do a video on multithreading?

  35. Palanivelu October 19, 2019 at 11:28 pm - Reply

    Best python teacher ever..

  36. Lucas Diz October 19, 2019 at 11:28 pm - Reply

    Corey, do you know HOW to program a payment system for the app at Flask ? Like the flaskblog, BUT if there was a payment to do for premium features for exemple. Do you know ? I mena, can using paypal.. Thanks for all the help and vids, you are literally the best.

  37. Fasil Minale October 19, 2019 at 11:28 pm - Reply

    0.75

  38. nelson 128 October 19, 2019 at 11:28 pm - Reply

    https://github.com/honeydatax please help to make a vedeo about my new operation system home made please thanks

  39. Andrey Shepeta October 19, 2019 at 11:28 pm - Reply

    Great tutorial! Magnificent library! Thank you, Coray! You are doing a great work!

Leave A Comment

*