![](/img/trans.png)
[英]How to invoke a specific Python version WITHIN a script.py — Windows
[英]How to use Popen in Windows to invoke an external .py script and wait for its completion
您是否嘗試過通過調用外部zip.py腳本來工作的反饋? 我的CGITB沒有顯示任何錯誤消息。 它只是沒有調用外部.py腳本即可工作。 它只是跳過而涌出。 如果您能協助我在feedback.py中將此zip.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)
zip()
是Python中的內置函數。 因此,使用zip
作為變量名是一個不好的做法。 可以使用zip_
代替。
execfile()
函數讀取並執行Python腳本。
可能實際上您實際上只需要在feedback.py中import zip_
而不是execfile()
。
Yay ArcGIS。
只是為了闡明您如何嘗試使用popen調用此腳本,您可以發布一些代碼嗎?
如果您是通過ArcGIS環境中的另一個腳本來調用此腳本的,那么事實是,當您使用Popen時,該腳本將不會在ArcGIS環境中被調用,而是會在Windows中被調用。 因此,您將失去對其的所有實際控制權。
同樣,只有另一個ArcGIS注釋,您永遠不會初始化地理處理器的許可證。
我的建議將您的代碼重構到一個模塊函數中,該函數僅嘗試壓縮文件,如果失敗則將消息打印到ArcGIS。
如果要發布您如何調用它以及如何運行它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.