Python Subprocess Call Example
Python Programming Language:
Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said to be a user-friendly programing language, as the syntax for it is very simple to write and easy to understand for a beginner programmer. Python programming language is rich in libraries that can be imported easily and used to perform many different operations. In the year 1989, Guido van Rossum is the one who introduced python programming language. It is also used in web applications; web applications like the Django and Flask frameworks are created using python. Compared to any programming language, the syntax in python is much easier.
Python programming language is most widely used language in today’s technology. Many colleges and institutions have introduced python in their syllabus so that the students need to learn python. The biggest advantage of the python programming language is that it has a good collection of libraries widely used in machine learning, web frameworks, test frameworks, multimedia, image processing, and many more applications. The latest version of the python programming language available is python 3 which is the most updated version of the python programming language.In a computer, everything that occurs is a process, to put it simply. You begin a new process each time you launch an application, issue a command-line command, or execute a Python script. Everything that happens on your computer, from displaying a menu bar to launching a complicated application, is a process.
For instance, when you run a Python script from the command line, a new process is started. Now, there is a relationship between these two processes: the process that creates another process is the parent, and the newly generated process is the child.
In a similar vein, a Python script has the ability to start a new process, at which point it takes on the role of the new process' parent. In this post, we'll look at how to launch various subprocesses within a standard Python script by using the subprocess module in Python.
Although this is not a technical essay, understanding the fundamentals of Python may be required in order to follow the examples and concepts.
Python Subprocess
A common Python module called the subprocess allows you to launch new processes from within a Python script. When you need to execute numerous processes simultaneously or call an external programme or external command from inside your Python code, it's quite beneficial and, in fact, it's the option that is advised.
The ability to manage inputs, outputs, and even errors generated by the child process from the Python code is one of the advantages of the subprocess module. This option increases the power and flexibility of calling subprocesses by allowing, for example, the use of the subprocess' output as a variable throughout the remainder of the Python script.
The module was initially developed in Python 2.4 as a replacement for existing functions like os.system. Additionally, as of Python 3.5, the run() function—the subject of this article—is the suggested method of using this module.
Example1:
importsubprocess
# Run a command with arguments
subprocess.call(["ls", "-l"])
# Capture output from a command
output = subprocess.check_output(["echo", "Hello World!"])
print(output)
# Run a command with environment variables
subprocess.call(["env"], env={"FOO": "bar"})
Example 2:
importsubprocess
# Run a command, capture the output
output = subprocess.check_output("echo hello world", shell=True)
print(output)
# Run a command, capture the exit code and output
process = subprocess.Popen("echo hello world", shell=True, stdout=subprocess.PIPE) exit_code = process.wait()
output = process.stdout.read()
print(output)
Example 3:
importsubprocess
# Running a shell command
subprocess.call("dir", shell=True)
#Passing command line arguments to a command
subprocess.call(["ls", "-l"])
# Capture output of a shell command
output = subprocess.check_output("dir", shell=True)
Conclusion
A common Python module called the subprocess allows you to launch new processes from within a Python script. When you need to execute numerous processes simultaneously or call an external programme or external command from inside your Python code, it's quite beneficial and, in fact, it's the option that is advised.The ability to manage inputs, outputs, and even errors generated by the child process from the Python code is one of the advantages of the subprocess module. This option increases the power and flexibility of calling subprocesses by allowing, for example, the use of the subprocess' output as a variable throughout the remainder of the Python script.