【问题标题】:How to run a Tcl Script in a folder in Python?如何在 Python 的文件夹中运行 Tcl 脚本?
【发布时间】:2014-07-17 10:41:08
【问题描述】:

我有一个 Tcl 脚本存储在 C:/ 如何从 Python 运行它? (不是用python写Tcl脚本然后运行)

为了澄清我的问题,首先我有一个名为 oommf 的基于 Tcl 的程序,用于模拟。这里简单介绍一下http://math.nist.gov/oommf/

我用这个程序写了一些脚本,想用python运行一系列模拟,然后提取数据绘制一些图形。 .mif 格式的脚本,我想做的是

  1. 使用python生成.mif脚本,每次使用不同的参数
  2. 使用python调用基于Tcl的程序运行脚本
  3. 保存数据并使用python提取和绘制图形

Tcl 程序为 .tcl 格式。

Tcl 程序也可以在命令行中运行。我听说有一些方法可以在python中模拟命令行(在windows环境下),但我不知道,如果有人知道,它会很有帮助。

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

【问题讨论】:

    标签: python tcl


    【解决方案1】:

    试试subprocess.call

    subprocess.call 将避免必须处理各种 shell 的引用约定的问题。它接受一个列表,而不是一个字符串,因此参数更容易分隔。即

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

    【讨论】:

    • 您应该更新您的答案,使其与问题更密切相关(即,tclsh.exe 而不是Notepad.exe,以及test.tcl 而不是test.txt)。
    • 感谢您的建议。我已经更新了我的问题,你能帮忙吗?
    • @littleHui : 你需要什么帮助?
    【解决方案2】:

    这是一个建议。大部分都是猜测,因为我不知道你想做什么。

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-05
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多