Python 3 Subprocess Module Tutorial to Run Terminal & Shell Commands Programmatically
183 views
Jun 3, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I will show you a very
0:04
important uh module inside Python which
0:07
lets you execute terminal and shell
0:09
commands programmatically using a Python
0:12
script so terminal commands are those
0:15
commands which you directly open your
0:17
command line and then execute those
0:19
commands so let's suppose if I want to
0:21
print out the version of which version
0:23
of Python is installed so I will
0:25
directly open command line and execute
0:28
this command and I will get this output
0:30
so now just imagine you want to execute
0:34
this same command not through directly
0:36
opening the command line rather than you
0:39
want to open a Python script which will
0:42
automatically do this for you
0:44
programmatically so in this video I will
0:46
show you a module here which is built in
0:48
inside Python which lets you do this so
0:51
the name of the module is subprocess so
0:54
first of all you just need to import
0:56
this module here like this subprocess so
0:59
it's a built-in module you did not have
1:01
to install it so you simply import this
1:04
module like this and after that you
1:06
simply
1:08
uh just call a simple function which is
1:10
present inside this module here which is
1:13
call so you simply call this function
1:17
like this and here you specify which
1:20
command you want to execute so inside
1:23
double quotes you simply write here
1:25
python and then put a comma and
1:28
then d- version so we are executing the
1:32
same command here you will see using
1:34
programmatically using a python script
1:37
using this mod module and uh then to
1:41
print out the result here we can simply
1:43
print out the
1:45
result so that's all that we need to do
1:48
right here and if I just execute this
1:51
python app py so you will actually
1:55
see it will print out and execute this
1:59
command and print out the result which
2:02
comes so you will see python
2:06
3.10.5 so it is printing out the exact
2:09
result that we if I try to print this
2:12
command
2:13
directly so in this is a really useful
2:17
and a powerful module as well because
2:19
many a times you need to execute the
2:21
shell commands or terminal commands
2:23
inside your Python script so if you're
2:26
making some kind of a terminal
2:28
application then this module is very
2:30
much helpful
2:32
subprocess so let's suppose you also
2:35
need to print out which version is uh
2:38
ffmppg in installed
2:41
so you can just write ffmpg dash version
2:45
so it will now also print out which
2:48
version of uh ffmppg installed so in
2:52
this way you can execute multiple
2:54
commands so if I just execute the python
2:56
script here you will see that first it
2:59
will print out the python version and
3:01
then it will go to the next command and
3:03
then it will print out the ffmpg version
3:06
so any command line utility kind of
3:09
application you can build using this
3:11
module and uh there is also an uh new
3:14
way which you can do the same
3:17
process uh the second way is to import
3:20
the operating system module and simply
3:24
calling the system method and here you
3:28
specify the same command here python-
3:31
version and
3:34
again so this this method is little bit
3:37
easier because uh the syntax is little
3:40
bit easier you will see how easy it is
3:43
to remember this
3:45
so I personally like this
3:47
method more as compared to the previous
3:50
one so you will see both the results
3:53
both the methods will return the same
3:55
result to you so you just need to decide
3:58
which one
3:59
to so if I just execute this now you
4:02
will see it will also return the same
4:04
result you will see that so these are
4:07
the two ways by which you can execute uh
4:09
terminal and
4:11
uh terminal commands inside your Python
4:14
application programmatically
4:17
so first through subprocess module and
4:20
secondly through operating system module
4:23
so thank you very much for watching this
4:25
video and also check out my website
4:28
freemediatoolsh.com uh which contains
4:30
thousands of tools
#Scripting Languages