How to call python script from Groovy and the python script in turn calls another python script

Hii,

I am trying to call the python script from groovy and passing some command line arguments along with the command.

The command I found to execute python code from groovy is:

Groovy code :

"python I:\Researchwork\scriptfile1.py ‘myname’ ‘count’ ".execute()

Python:

In my python script file, I am taking the arguments and making a text file using this arguments. After this I am calling one more script file from the same file.

Code of python script:
scriptfile1.py


import sys, optparse, os

import subprocess

if __name__ == '__main__':

    # Count the arguments

    open('file.txt', 'w').close()

    with open('file.txt', 'a') as file:        

        file.write("\n"+sys.argv[1])

        file.write("\n"+sys.argv[2])

   

    subprocess.call("python main1.py 'file.txt' 'foldername' ", shell=True)

    print("Done")

main1.py :

This file takes in the file.txt and foldername and does my work

Here file.txt is a file where I am storing the text and passing this file.txt name and the foldername to call my second python script file using subprocess.call()

My problem is,

  • The groovy code is able to call the python script but the file.txt is not updating with the text from command line argument
  • The sub script in python script file is also not executing(i.e., main1.py file is not executing)

Please Kindly help me to solve the issue.

Thanks & Regards,
Jyothi

Hi @Jyothi_R,

I’ve never tried to call a Python script from Groovy before… But QuPath can be called directly from the command line and can run Groovy script from there as well. If you were to ‘translate’ your Python script to Groovy maybe this will be an easier and cleaner solution to run your tasks?

1 Like

My problem is solved.
Thanks for the ones who had a look at my problem to help me.

Thanks & Regards,
Jyothi