exec命令运行python脚本

exec命令运行python脚本

本文介绍了如何使用tcl exec命令运行python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tcl驱动程序脚本,该脚本依次调用其他几个程序.我想从我的tcl脚本中调用python脚本.可以说这是我的python脚本"1.py"

I have a tcl driver script which in turn calls several other programs.I want to invoke a python script from my tcl script.lets say this is my python script "1.py"

#!/usr/bin/python2.4
import os
import sys
try:
    fi = open('sample_+_file', 'w')
except IOError:
    print 'Can\'t open file for writing.'
    sys.exit(0)

tcl脚本为"1.tcl"

and tcl script is "1.tcl"

#! /usr/bin/tclsh
proc call_python {} {
    exec python 1.py
}

这不会产生任何错误,但同时不会执行python脚本中存在的操作.

This doesn't give any error but at the same time it does not perform the operations present in the python script.

应该用什么替换1.tcl中的代码片段"exec python 1.py"来调用python脚本?可以使用exec调用python脚本吗?

What should replace the code fragment "exec python 1.py" in 1.tcl to invoke the python script? Can a python script be invoked using exec?

提前谢谢!

推荐答案

您的tcl脚本定义了执行python脚本的过程,但未调用该过程.将呼叫添加到您的tcl脚本:

Your tcl script defines a procedure to execute a python script, but does not call the procedure. Add a call to your tcl script:

#! /usr/bin/tclsh
proc call_python {} {
    set output [exec python helloWorld.py]
    puts $output
}

call_python

此外,通过exec启动的进程写入stdout的任何内容都不会显示在终端中.您需要从exec调用中捕获它,并且显式地自己打印:

Also, anything written to stdout by the process launched via exec will not get displayed in your terminal. You'll need to capture it from the exec call, and print is explicitly yourself:

#! /usr/bin/tclsh
proc call_python {} {
    set output [exec python helloWorld.py]
    puts $output
}

call_python

这篇关于如何使用tcl exec命令运行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:06