matlab - Python subprocess.call doesn't wait for command to execute -


i'm new python, need use assignment in course. developed solution (an optimization algorithm) in freemat / octave / matlab .m file , wanted call python (the python code called grading python script).

the .m file reads file called tmp.data , writes output output.txt. python script should read output , convert result grading script expects.

all runs fine, except haven't been able make python wait call matlab complete , therefore generates error on following lines.

here's code:

#!/usr/bin/python # -*- coding: utf-8 -*-  collections import namedtuple item = namedtuple("item", ['index', 'value', 'weight'])  import subprocess import os subprocess import popen, pipe  def solve_it(input_data):     # modify code run optimization algorithm      # write inputdata temporay file     tmp_file_name = 'tmp.data'     tmp_file = open(tmp_file_name, 'w')     tmp_file.write(input_data)     tmp_file.close()      # call matlab (or other solver)     # subprocess.call('matlab -r gp(\'tmp.data\')', shell=1)     # run=os.system     # a=run('matlab -r gp(\'tmp.data\')')     # process = popen('matlab -r gp(\'tmp.data\')', stdout=pipe)     # popen.wait()     # (stdout, stderr) = process.communicate()     subprocess.call('matlab -r gp(\'tmp.data\')',shell=0)      # read result file     open('output.txt') f:         result = f.read()      # remove temporay file     os.remove(tmp_file_name)     os.remove('output.txt')      return result         # return stdout.strip()        # prepare solution in specified output format     # output_data = str(value) + ' ' + str(0) + '\n'     # output_data += ' '.join(map(str, taken))     # return output_data   import sys  if __name__ == '__main__':     if len(sys.argv) > 1:         file_location = sys.argv[1].strip()         input_data_file = open(file_location, 'r')         input_data = ''.join(input_data_file.readlines())         input_data_file.close()         print solve_it(input_data)     else:         print 'this test requires input file.  please select 1 data directory. (i.e. python solver.py ./data/ks_4_0)' 

as see, i've tried subprocess.call, popen, os.system... no avail. of them give me similar errors:

c:\users\gp\documents\documents\personal\educacion\discrete optimization\knapsack>python2 solver.py data/ks_19_0 traceback (most recent call last):   file "solver.py", line 60, in <module>     print solve_it(input_data)   file "solver.py", line 30, in solve_it     open('output.txt') f: ioerror: [errno 2] no such file or directory: 'output.txt' 

of course! error comes while matlab still in process of opening. trying access file hasn't been created yet.

what should python wait matlab complete??

i appreciate kind help, thanks.

your code seems irgnore fact matlab uses launcher (matlab_root/bin/matlab.exe) , main application (matlab_root/bin/xxx/matlab.exe). keep launcher open until main application closes, have use -wait option.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -