user1763370
user1763370

Reputation:

Chaining Python Scripts

I have two user defined python scripts. First takes a file and processes it, while the second script takes the output of first and runs an executable, and supplies the output of first script to program with additional formatting.

I need to run these scripts via another python script, which is my main executable script.

I searched a bit about this topic and;

  1. I can use importlib to gather the content of scripts so that I can call them at appropriate times. This requires the scripts to be under my directory/or modification to path environment variable. So it is a bit ugly looking at best, not seem pythonish.
  2. Built-in eval function. This requires the user to write a server-client like structure, cause the second script might have to run the said program more than one time while the first script still gives output.

I think I'm designing something wrong, but I cannot come up with a better approach.

A more detailed explenation(maybe gibberish)

I need to benchmark some programs, while doing so I have a standard form of data, and this data needs to be supplied to benchmark programs. The scripts are (due to nature of benchmark) special to each program, and needs to be bundled with benchmark definition, yet I need to create this program as a standalone configurable tester. I think, I have designed something wrong, and would love to hear the design approaches.

PS: I do not want to limit the user, and this is the reason why I choose to run python scripts.

Upvotes: 1

Views: 1612

Answers (1)

R. Wayne
R. Wayne

Reputation: 407

I created a few test scripts to make sure this works. The first one (count_01.py) sleeps for 100 seconds, then counts from 0 to 99 and sends it to count_01.output. The second one (count_02.py) reads the output of first one (count_01.output) and adds 1 to each number and writes that to count_02.output. The third script (chaining_programs.py) runs the first one and waits for it to finish before calling the second one.

# count_01.py --------------------
from time import sleep

sleep(100)

filename = "count_01.output"
file_write = open(filename,"w")
for i in range(100):
    #print " i = " + str(i)
    output_string = str(i)
    file_write.write(output_string)
    file_write.write("\n")
file_write.close()
# ---------------------------------

# count_02.py --------------------
file_in = "count_01.output"
file_out = "count_02.output"
file_read = open(file_in,"r")
file_write = open(file_out,"w")
for i in range(100):
    line_in = file_read.next()
    line_out = str(int(line_in) + 1)
    file_write.write(line_out)
    file_write.write("\n")
file_read.close()
file_write.close()
# ---------------------------------

# chaining_programs.py -------------------------------------------------------
import subprocess
import sys
#-----------------------------------------------------------------------------
path_python = 'C:\Python27\python.exe' # 'C:\\Python27\\python.exe'
#
# single slashes did not work
#program_to_run = 'C:\Users\aaaaa\workspace\Rich_Project_044_New_Snippets\source\count.py'
program_to_run_01 = 'C:\\Users\\aaaaa\\workspace\\Rich_Project_044_New_Snippets\\source\\count_01.py'
program_to_run_02 = 'C:\\Users\\aaaaa\\workspace\\Rich_Project_044_New_Snippets\\source\\count_02.py'
#-----------------------------------------------------------------------------
# waits
sys.pid = subprocess.call([path_python, program_to_run_01])
# does not wait
sys.pid = subprocess.Popen([path_python, program_to_run_02])
#-----------------------------------------------------------------------------

Upvotes: 0

Related Questions