AspiringMat
AspiringMat

Reputation: 2279

How to run one python file from another python file?

To start off, I am a beginner in python so I am not even sure if my question makes sense or is even possible.

I have 2 python files app.py. and compare.py. compare.py takes in two arguments (File paths) to run. So for example, when I want to run it, I do python compare.py ./image1.jpg ./image2.jpg. Now the return I get is some text printed to the terminal such as Comparison Done, The distance is 0.544.

Now, I want to run this compare.py from inside app.py and get a string with whatever compare.py would usually output to the terminal. So for example:

result = function('compare.py ./image1.jpg ./image2.jpg') and result will have the required string. Is this possible?

Upvotes: 0

Views: 104

Answers (1)

Ajax1234
Ajax1234

Reputation: 71471

You can use os.popen:

In app.py:

import os
output = os.popen('python compare.py ./image1.jpg ./image2.jpg').readlines()

Upvotes: 2

Related Questions