简体   繁体   English

如何在 Python 的文件夹中运行 Tcl 脚本?

[英]How to run a Tcl Script in a folder in Python?

I have a Tcl script stored in C:/ How do I run it from Python?我有一个 Tcl 脚本存储在 C:/ 如何从 Python 运行它? (It is not write the Tcl script in python and then run it) (它不是在python中编写Tcl脚本然后运行它)

To clarify my question, firstly I have a Tcl-based programme called oommf, which is used for simulation.为了澄清我的问题,首先我有一个名为 oommf 的基于 Tcl 的程序,用于模拟。 Here is a short introduction http://math.nist.gov/oommf/这是一个简短的介绍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.我用这个程序写了一些脚本,想用python来运行一系列的模拟,然后提取数据来绘制一些图形。 The script in in .mif format, and what I wish to do is .mif 格式的脚本,我想做的是

  1. Use python to generate the .mif script, every time with different parameter使用 python 生成 .mif 脚本,每次使用不同的参数
  2. Use python to call the Tcl-based programme to run the script使用python调用基于Tcl的程序运行脚本
  3. Save the data and use python to extract and plot the graph保存数据并使用python提取并绘制图形

The Tcl programme is in .tcl format. Tcl 程序采用 .tcl 格式。

The Tcl programme can also be run in command line. Tcl 程序也可以在命令行中运行。 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.我听说有一些方法可以在 python 中模拟命令行(在 Windows 环境中),但我不知道,如果有人知道,它会很有帮助。

(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) (对不起,我之前的编程知识只有一点 C,这就是为什么我的问题可能含糊不清,因为我不知道如何更清楚地描述它)

Try with subprocess.call尝试使用subprocess.call

subprocess.call will avoid problems with having to deal with quoting conventions of various shells. subprocess.call 将避免必须处理各种 shell 的引用约定的问题。 It accepts a list, rather than a string, so arguments are more easily delimited.它接受一个列表,而不是一个字符串,所以参数更容易分隔。 ie IE

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

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM