[英]How to run a Tcl Script in a folder in Python?
我有一个 Tcl 脚本存储在 C:/ 如何从 Python 运行它? (它不是在python中编写Tcl脚本然后运行它)
为了澄清我的问题,首先我有一个名为 oommf 的基于 Tcl 的程序,用于模拟。 这是一个简短的介绍http://math.nist.gov/oommf/
我用这个程序写了一些脚本,想用python来运行一系列的模拟,然后提取数据来绘制一些图形。 .mif 格式的脚本,我想做的是
Tcl 程序采用 .tcl 格式。
Tcl 程序也可以在命令行中运行。 我听说有一些方法可以在 python 中模拟命令行(在 Windows 环境中),但我不知道,如果有人知道,它会很有帮助。
(对不起,我之前的编程知识只有一点 C,这就是为什么我的问题可能含糊不清,因为我不知道如何更清楚地描述它)
尝试使用subprocess.call
subprocess.call 将避免必须处理各种 shell 的引用约定的问题。 它接受一个列表,而不是一个字符串,所以参数更容易分隔。 IE
import subprocess
subprocess.call(['C:\\Temp\\a b c\\Notepad.exe', 'C:\\test.txt'])
这是一个建议。 大部分都是猜测,因为我不知道你到底想做什么。
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.