How to run a Tcl Script in a folder in Python?

人盡茶涼 提交于 2020-01-22 03:52:07

问题


I have a Tcl script stored in C:/ How do I run it from Python? (It is not write the Tcl script in python and then run it)

To clarify my question, firstly I have a Tcl-based programme called oommf, which is used for simulation. Here is a short introduction http://math.nist.gov/oommf/

I wrote some scripts by this programme and would like to use python to run a series of simulation and then extract the data to plot some graph. The script in in .mif format, and what I wish to do is

  1. Use python to generate the .mif script, every time with different parameter
  2. Use python to call the Tcl-based programme to run the script
  3. Save the data and use python to extract and plot the graph

The Tcl programme is in .tcl format.

The Tcl programme can also be run in command line. I heard that there was some way to simulate command line in python(in windows environment), but I don't know and if any one knows, it would help much.

(Sorry my prior knowledge for programming is only a bit in C, that's why my question might be ambiguous because I don't know how to describe it more clearly)


回答1:


Try with subprocess.call

subprocess.call will avoid problems with having to deal with quoting conventions of various shells. It accepts a list, rather than a string, so arguments are more easily delimited. i.e.

import subprocess
subprocess.call(['C:\\Temp\\a b c\\Notepad.exe', 'C:\\test.txt'])



回答2:


Here is a suggestion. Much of it is guessing since I don't know exactly what you want to do.

import subprocess

# 1. Code to generate .mif script, for example: my.mif
with open('my.mif', 'wb') as f:
    f.write('something')

# 2. Launch oommf. I am guessing the command line, based on your 
# description and based on what the location of tclsh in my system
cmd = ['C:/Tcl/bin/tclsh.exe', 'C:/oomf.tcl', 'my.mif']
process = subprocess.Popen(cmd, 
        stdout=subprocess.PIPE, 
        stderr=subprocess.PIPE)
stdout, stderr = process.communicate()

# 3. Now, you can do something with stdout and stderr if needed
# Below is just an example
with open('data.txt', 'wb') as f:
    f.write(stdout)


来源:https://stackoverflow.com/questions/23907698/how-to-run-a-tcl-script-in-a-folder-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!