[英]Calling python script with parameters from tcl
I want to execute a python function that have 2 parameters in my TCL program. 我想在我的TCL程序中执行具有2个参数的python函数。 My python function for dezipping a file.zip is defined by this code:
我的用于解压缩file.zip的python函数由以下代码定义:
# -*- coding: utf8 -*-
import zipfile
import os.path
import os
def dezipFile(filezip, pathdst = ''):
if pathdst == '': pathdst = os.getcwd()
zfile = zipfile.ZipFile(filezip,'r')
for i in zfile.namelist():
print (i)
if os.path.isdir(i):
try: os.makedirs(pathdst + os.sep + i)
except: pass
else:
try: os.makedirs(pathdst + os.sep + os.path.dirname(i))
except: pass
data = zfile.read(i)
fp = open(pathdst + os.sep + i, "wb")
fp.write(data)
fp.close()
zfile.close()
Then, I want to execute this function in my TCL program. 然后,我想在我的TCL程序中执行此功能。 I have used
exec
. 我用过
exec
。 TCL script: TCL脚本:
puts "--outzip is: $outZip"
puts "--destinationDeZip is: $destinationDeZip"
set zip_status [ catch {exec python dezipFile.py ${outZip} ${destinationDeZip}} id ]
if {$zip_status == 1} {
puts "**** Error: $id *********"
} else {
puts "**** Pass: the zipped file $fileName is extracted *********"
}
In the result report, I have found the message **** Pass: the zipped file $fileName is extracted *********
. 在结果报告中,我发现消息
**** Pass: the zipped file $fileName is extracted *********
。 The command seems to be passed, however the the zipped files are not extracted as expected. 该命令似乎已传递,但是未按预期提取压缩文件。 Is there any error in my program?
我的程序有错误吗?
Thks. THKS。
You need some extra code on the Python side to actually call the function you defined. 您需要在Python端添加一些额外的代码才能实际调用您定义的函数。
if __name__ == '__main__':
import sys
dezipFile(*sys.argv[1:])
The sys.argv[1:]
gets all the arguments after the script name, and doing dezipFile(*…)
passes all of those into the dezipFile
function as separate arguments. sys.argv[1:]
在脚本名称之后获取所有参数,然后执行dezipFile(*…)
将所有这些参数作为单独的参数传递给dezipFile
函数。
This is not meant as an answer, more as a suggestion for improvement: I assume you are aware that you do not have resort to Python to decompress and access the content of a ZIP archive in Tcl? 这不是要给出答案,而是要提出改进建议:我假设您知道您没有使用Python解压缩和访问Tcl中ZIP归档文件的内容吗?
You might want to consider using the tcllib
module zipfile::decode
, for a start: 您可能需要考虑使用
tcllib
模块zipfile::decode
作为开始:
package require Tcl 8.6
package require zipfile::decode
try {
::zipfile::decode::unzipfile $outZip $destinationDeZip
} on error {errMsg opts} {
# ...
}
(This assumes Tcl 8.6 being available to you.) (这假定您可以使用Tcl 8.6。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.