Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run python scripts using tcl exec command

Tags:

python

exec

tcl

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)

and tcl script is "1.tcl"

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

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

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?

Thanks in advance!!

like image 769
usha Avatar asked Jul 26 '11 05:07

usha


People also ask

How do I run a Python script in Tcl?

use "exec" and pass the full path to the python script.

How do you execute a command in Tcl?

The exec command runs programs from your Tcl script. For example: Unlike other UNIX shell exec commands, the Tcl exec does not replace the current process with the new one. Instead, the Tcl library forks first and executes the program as a child process.

How do I execute a Python script?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World! If everything works okay, after you press Enter , you'll see the phrase Hello World!


1 Answers

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

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
like image 93
Ergwun Avatar answered Oct 09 '22 14:10

Ergwun