如何在Windows中使用Popen调用外部.py脚本并等待完成
你有没有试过在反馈中调用一个外部的 zip.py 脚本?我的 CGITB 没有显示任何错误信息。它就是没有执行这个外部的 .py 脚本,直接跳过了。我很感激如果你能帮我让这个 zip.py 在 feedback.py 中可以被调用。
谢谢。大卫
#**********************************************************************
# Description:
# Zips the contents of a folder.
# Parameters:
# 0 - Input folder.
# 1 - Output zip file. It is assumed that the user added the .zip
# extension.
#**********************************************************************
# Import modules and create the geoprocessor
#
import sys, zipfile, arcgisscripting, os, traceback
gp = arcgisscripting.create()
# Function for zipping files. If keep is true, the folder, along with
# all its contents, will be written to the zip file. If false, only
# the contents of the input folder will be written to the zip file -
# the input folder name will not appear in the zip file.
#
def zipws(path, zip, keep):
path = os.path.normpath(path)
# os.walk visits every subdirectory, returning a 3-tuple
# of directory name, subdirectories in it, and filenames
# in it.
#
for (dirpath, dirnames, filenames) in os.walk(path):
# Iterate over every filename
#
for file in filenames:
# Ignore .lock files
#
if not file.endswith('.lock'):
gp.AddMessage("Adding %s..." % os.path.join(path, dirpath, file))
try:
if keep:
zip.write(os.path.join(dirpath, file),
os.path.join(os.path.basename(path),
os.path.join(dirpath, file)[len(path)+len(os.sep):]))
else:
zip.write(os.path.join(dirpath, file),
os.path.join(dirpath[len(path):], file))
except Exception, e:
gp.AddWarning(" Error adding %s: %s" % (file, e))
return None
if __name__ == '__main__':
try:
# Get the tool parameter values
#
infolder = gp.GetParameterAsText(0)
outfile = gp.GetParameterAsText(1)
# Create the zip file for writing compressed data. In some rare
# instances, the ZIP_DEFLATED constant may be unavailable and
# the ZIP_STORED constant is used instead. When ZIP_STORED is
# used, the zip file does not contain compressed data, resulting
# in large zip files.
#
try:
zip = zipfile.ZipFile(outfile, 'w', zipfile.ZIP_DEFLATED)
zipws(infolder, zip, True)
zip.close()
except RuntimeError:
# Delete zip file if exists
#
if os.path.exists(outfile):
os.unlink(outfile)
zip = zipfile.ZipFile(outfile, 'w', zipfile.ZIP_STORED)
zipws(infolder, zip, True)
zip.close()
gp.AddWarning(" Unable to compress zip file contents.")
gp.AddMessage("Zip file created successfully")
except:
# Return any python specific errors as well as any errors from the geoprocessor
#
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo +
"\nError Info:\n " + str(sys.exc_type) +
": " + str(sys.exc_value) + "\n"
gp.AddError(pymsg)
msgs = "GP ERRORS:\n" + gp.GetMessages(2) + "\n"
gp.AddError(msgs)
2 个回答
0
太好了,ArcGIS。
我想确认一下,你是怎么用popen来调用这个脚本的?能不能发点代码过来?
如果你是在ArcGIS环境中通过另一个脚本来调用这个脚本,那么问题来了,当你使用Popen时,这个脚本并不是在ArcGIS环境中运行,而是在Windows环境中运行。所以你就失去了对它的真正控制。
还有一点,关于ArcGIS,你从来没有为地理处理器初始化许可证。
我的建议是,把你的代码重构成一个模块函数,这个函数简单地尝试压缩文件,如果失败了,就把错误信息打印到ArcGIS里。
如果你愿意,可以发一下你是怎么调用这个脚本的,以及它是怎么运行的。
1
zip()
是 Python 里自带的一个函数。所以用zip
作为变量名是不太好的习惯。可以用zip_
来代替。execfile()
函数可以读取并执行一个 Python 脚本。你可能只需要在 feedback.py 里用
import zip_
,而不是用execfile()
。