sarad
sarad

Reputation: 117

How to execute python script from tcl script with argument

I have the following tcl script:

puts "The total number of arguments is $argc"
if {$argc > 0} {puts "The arguments in ARGV are: $argv" }
exec python  $argv/../scripts/sim/sim_comp_gen.py

How can I exec the python script ans pass it the $argv?

Upvotes: 1

Views: 2805

Answers (2)

Mr. Bordoloi
Mr. Bordoloi

Reputation: 80

Donal's solution is the best. You can try out below as well.

exec echo "python_script_name.py argument1 argument2" > ./tempfile
exec chmod +x ./tempfile
exec ./tempfile

Let me know if it works.

Upvotes: 1

Donal Fellows
Donal Fellows

Reputation: 137577

The {*} syntax is key here:

exec python ../scripts/sim/sim_comp_gen.py {*}$argv

If you're making it relative to the name of the script, that's in $argv0 and Tcl has some built-in utilities for making that easier:

set script [file join [file dirname $argv0] scripts/sim/sim_comp_gen.py]
exec python $script {*}$argv

You might need file nativename on Windows. (I can't remember if python does the slash-type ignoring/conversion.)

set script [file join [file dirname $argv0] scripts/sim/sim_comp_gen.py]
exec python [file nativename $script] {*}$argv

On a very old version of Tcl that doesn't support {*}? Use eval carefully

set script [file join [file dirname $argv0] scripts/sim/sim_comp_gen.py]
eval [list exec python $script] $argv

The key is to use list to de-fang things. That works even when you've got tricky things like spaces in filenames.

Upvotes: 2

Related Questions