python subprocess

Adarsha Regmi
1 min readApr 29, 2022


ideas to run the code and application in different process


The subprocess is a module available in python language where new code and applications can be run. It has the flexibility to start a new application from python program itself. It can solve different problem we programmer come across. We can run different programs including C programs too. It returns the exit codes, inputs and outputs .

Starting a subprocess in python ?

To start a new process, you can use Popen function. The args includes program you want to start, second is file.

from subprocess import Popen, PIPEprocess = Popen(['cat', ''], stdout=PIPE, stderr=PIPE)stdout, stderr = process.communicate()print(stdout)

Calling the above will run the command and print the exit code and outputs.

All Together, the command for running a simple .java file using subprocess

class Hello{public static void main(String args[]){System.out.print("Java says Hello World!");}}

Running with py

import subprocessimport osdef Java_Execution():# storing the outputs = subprocess.check_output("javac;java Hello", shell = True)print(s.decode("utf-8"))# Driver functionsif __name__=="__main__":Java_Execution()

The above code will run .java file and print output.