-2

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)

2 Answers 2

0

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'])
Sign up to request clarification or add additional context in comments.

3 Comments

You should update your answer to be more closely relevant to the question (i.e., tclsh.exe instead of Notepad.exe, and test.tcl instead of test.txt).
Thanks for your advice. I have updated my question and could you help a bit?
@littleHui : what help do you need ?
0

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)

Comments

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.