9

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!!

4
  • Why not just use Python everywhere? Commented Jul 26, 2011 at 6:56
  • could have been used! But at this point of time, I want to reuse the already existing tcl driver that I have. Commented Jul 26, 2011 at 7:54
  • 1
    @Keith why not use TCL everywhere? Commented Jul 27, 2011 at 6:45
  • @hm, Ferrari or volkswagon... what will I choose? Commented Jul 27, 2011 at 9:10

1 Answer 1

18

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
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, very useful script! Is there a way to pass some variables to the python file?
can't find the file even when giving the full path: /web/projop/packages/intranet-timesheet2/www/hours/y.py

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.