0

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?

3
  • 1
    Without knowing more about the structure of compare.py, it's hard to say - it's possible, if it's sensibly written, that you can simply import the function with the appropriate behaviour and call it yourself. Commented Oct 11, 2017 at 11:59
  • Everything is possible as long as you put your mind to it ;) You may have to return these strings from one of the python functions and import one .py file in the other to call that function . Yea, but hard to say without knowing the structure of these files. Commented Oct 11, 2017 at 12:02
  • Do you have control over the compare script? It might be nicer to import the compare script and call the compare functions directly. Commented Oct 11, 2017 at 13:56

1 Answer 1

2

You can use os.popen:

In app.py:

import os
output = os.popen('python compare.py ./image1.jpg ./image2.jpg').readlines()
Sign up to request clarification or add additional context in comments.

1 Comment

@AspiringMat glad to help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.