Python 如何在Windows中使用Popen调用外部.py脚本并等待其完成
您是否尝试过调用外部zip.py脚本来执行此反馈?我的CGITB没有显示任何错误消息。它只是没有调用external.py脚本来工作。它只是简单地跳过去喷涌。如果您能帮助我在feedback.py中调用此zip.py,我将不胜感激 问候。大卫Python 如何在Windows中使用Popen调用外部.py脚本并等待其完成,python,windows,Python,Windows,您是否尝试过调用外部zip.py脚本来执行此反馈?我的CGITB没有显示任何错误消息。它只是没有调用external.py脚本来工作。它只是简单地跳过去喷涌。如果您能帮助我在feedback.py中调用此zip.py,我将不胜感激 问候。大卫 #********************************************************************** # Description: # Zips the contents of a folder. # Par
#**********************************************************************
# 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)
是Python中的内置函数。因此,使用zip()
作为变量名是一种不好的做法<代码>邮政编码可用于代替zip
函数读取并执行Python脚本execfile()
- 可能您实际上只需要在feedback.py中导入zip文件,而不需要导入execfile()
是Python中的内置函数。因此,使用zip()
作为变量名是一种不好的做法<代码>邮政编码可用于代替zip
函数读取并执行Python脚本execfile()
- 可能您实际上只需要在feedback.py中导入zip文件,而不需要导入execfile()
- 是啊,阿尔吉斯
为了澄清您是如何使用popen调用此脚本的,您可以发布一些代码吗
如果您通过ArcGIS环境中的另一个脚本调用此脚本,那么问题是,当您使用Popen时,脚本不会在ArcGIS环境中调用,而是会在windows中调用。所以你会失去对它的真正控制
还有一条ArcGIS注释:您永远不会初始化地理处理器的许可证
我的建议是,将代码重构为一个模块函数,如果无法将消息打印到ArcGIS,该函数只会尝试压缩文件
如果您想发布您是如何调用它的,以及它是如何运行的。Yay ArcGIS
为了澄清您是如何使用popen调用此脚本的,您可以发布一些代码吗
如果您通过ArcGIS环境中的另一个脚本调用此脚本,那么问题是,当您使用Popen时,脚本不会在ArcGIS环境中调用,而是会在windows中调用。所以你会失去对它的真正控制
还有一条ArcGIS注释:您永远不会初始化地理处理器的许可证
我的建议是,将代码重构为一个模块函数,如果无法将消息打印到ArcGIS,该函数只会尝试压缩文件
如果你想发布你是如何称呼它的,以及它是如何运行的。对不起,我真的不明白这个问题。什么是feedback.py?我觉得好像错过了什么。-1:太多的代码,太少的问题。怎么了?对不起,我真的不明白这个问题。什么是feedback.py?我觉得好像错过了什么。-1:太多的代码,太少的问题。有什么问题吗?