At the Spatial Analysis and Geoprocessing island at this year’s user conference, several folks asked us about running a Python script or ModelBuilder model at a prescribed time — usually in the early morning when their computers are bored and just waiting for
something to do. We thought it would be good to make a post about this and to share some tips.
The first place to start is with the help topicScheduling
a Python script to run at prescribed times. This topic is kind of buried in the help system so it’s easy to miss. The meat of this topic shows you how to launch Window’sTask
Scheduleron various operating systems (Windows 7, XP, and so on). The screenshot below shows theTask Scheduleron Windows 7. There’s a lot
stuff you can do with the task scheduler and you’ll just have to play around with it and read the Windows help. TheActionspane, on the right,
has theCreate Basic Taskaction, and this is the place to start.
Clicking onCreate Basic Taskopens a wizard where you define the name of your task, thetrigger(when
it runs), and theaction(what program to run). The screenshot below shows theActiontab
where you specify the name of your Python script to run as well as any arguments to the script.
TheActiontab is where we have a few tips, as follows.
Run the Python executable with arguments
To ensure that your Python script will run regardless of the login account that the schedule task uses, and to avoid any confusion about which version of Python is used in mixed environments (64bit or 32bit), we recommend that you run the Python executable
with the name of your Python file as an argument to the executable.
Suppose the script you want to run isE:\My script.py
. Instead
of running the script directly, instruct the task scheduler to runpython.exe
with
the script as an argument. For example:
C:\Python27\ArcGIS10.2\python.exe "E:\My script.py"
The location ofpython.exe
depends on your install. If you
don’t know where it is, you can discover its location; copy and paste the following code into a new Python script then execute the script. The script will print the location ofpython.exe
as
well as other information about your Python environment.
import sys
import platform
import imp
print("Python EXE : " + sys.executable)
print("Architecture : " + platform.architecture()[0])
print("Path to arcpy : " + imp.find_module("arcpy")[1])
raw_input("\n\nPress ENTER to quit")
After determining the location ofpython.exe
, this is what
is entered in theActionpanel of the task scheduler:
If there are additional arguments (parameters) to your script, provide them after the path to your script. For example, the following command line executes a Python script that takes two arguments; the path to a feature class and an integer.
C:\Python27\ArcGIS10.2\python.exe "E:\My Scripts\DoConversion.py" c:\gisWork\gdb.mdb\counties 10
We typically recommend writing scripts so that they take arguments like the above example rather the hard-coding dataset paths or values within the script. However, this is one case where hard-coding paths and values in your script works to
your advantage; rather than changing argument values in the task scheduler, it’s easier to just edit your script to use different paths or values.
If you’re familiar with.bat
files in Windows, you can have
the task scheduler run a.bat
file that in turn runs your
script with arguments. The contents of the.bat
is the single
command line shown above.
Remember that if any of your arguments contain spaces, you need to enclose the argument in double quotes. In the above example,E:\My
Scripts\DoConversion.py
contains a space, so double quotes are required.
Running models at a prescribed time
A common misconception is that in order to run a model as a Python script, you must first export the model to a Python script. Geoprocessing is designed so that models are tools, just like all the tools delivered with the ArcGIS. That means you can run your
model within Python just like any other geoprocessing tool. All you need to do is import the toolbox containing your model using theImportToolboxfunction,
then run your model within Python.
For example, the model to the left imports a.gpx
file to
a geodatabase, adds a field, calculates it to the current date and time, then appends all those features into a “master” feature class.
To run this model on a schedule, all that’s needed is to execute the model within a script. Here’s the Python script to run the model:
importarcpy
importos
fromdatetime importdatetime
# Import the toolbox containing the model. This toolbox
# has an alias of "gpxtools"
#
arcpy.ImportToolbox(r"c:\importgpx\myGPXstuff.tbx")
# Run the model. The model has two parameters, the input
# .gpx file and the feature class to update
#
arcpy.ImportGPX_gpxtools(r"c:\ftp\inbox\datadrop.gpx",
r"c:\ftp\server.sde\gpx_tracks")
# Now that is has been processed, rename input gpx
# to have the date and time.
#
os.rename(r"c:\ftp\inbox\datadrop.gpx",
r"c:\ftp\inbox\datadrop{}.gpx".format(datetime.strftime(datetime.now(),"%Y%m%d"))
Advanced options
TheCreate Basic Taskwizard allows you to set basic properties of the task. Once the task is created, you can examine its properties and refine its triggers and actions. For
example, you can have your task run every five minutes for three hours. You can set up a task with advanced options using theCreate Taskaction.
Start simple
When I started writing this blog, it had been a long time since I messed around with scheduled tasks, so I started with a simple script that wrote information to a log file. I scheduled it to run every minute for ten minutes so I could test changes to the script
soon after I made them. It was a great way to test and understand how scheduling worked before committing to running the “real” script. Here’s my simple script:
importsys
importplatform
importimp
print"Importing arcpy... this may take a moment\n"
importarcpy
# Open a log file to write to
#
f=open(r'E:\schedule.log','w')
# Write date/time
#
f.write(time.strftime('%x %X'))
f.write('\n')
# Test receiving of arguments/parameters via arcpy
#
f.write("Parameter 0 : " + arcpy.GetParameterAsText(0)+"\n")
# Python info
#
f.write("Python EXE : " + sys.executable +"\n")
f.write("Architecture : " + platform.architecture()[0]+"\n")
f.write("Path to arcpy : " + imp.find_module("arcpy")[1]+"\n")
Ghislain Prince contributed to this post
分享到:
相关推荐
SecureCRT运行Python脚本测试IoT-WiFi模块-基础1 讲解基础的CRT-Python
v2ex 定时自动签到脚本
Python脚本,批量执行数据库脚本,适用于SQL Server数据库,后续会上传MySQL的批量执行sql脚本
用python脚本过滤文本中的关键字并显示,例子
统信桌面操作系统PXE部署Python脚本 统信桌面操作系统PXE部署Python脚本 统信桌面操作系统PXE部署Python脚本
面向Arcgis的python脚本编程
python脚本python脚本python脚本python脚本python脚本
Python 脚本可自动执行 Simulia Abaqus 日常使用中的枯燥工作,从打开 GUI 到控制参数分析。 启动 Abaqus CAE open_abaqus.bat 启动 Abaqus CAE 并调用 open_abaqus.py 脚本。您可以为 bat 文件创建一个链接并将其...
主要给大家介绍了关于ubuntu定时执行python脚本的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用ubuntu具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
3dSlier是一款医学图像处理的软件...因为最近在学习3DSlicer 的Python脚本编程,但是却因为官网的教程太过于零散而感到苦恼,所以特意仔细的整理了3Dslicer官网上有关Python脚本编程的资料。贡献出来,供大家使用学习。
本资源为一个完整工程,功能是实现使用C++在执行过程中调用Python脚本,内附VS嵌入Python环境的流程
本文档主要讲述的是Python脚本使用详解;
python脚本
一、Python开机自动运行 假如Python自启动脚本为 auto.py 。那么用root权限编辑以下文件...二、让Python脚本定时启动 用root权限编辑以下文件 sudo vim /etc/crontab 在文件末尾添加以下命令 2 * * * * root /usr/bi
主要介绍了CentOS 7如何实现定时执行python脚本,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
于是决定用Python写一个自动发送微信信息的脚本,第二天早上自动发送。 任务简述:使用Python自动化程序,把file.txt在早上6点准时拖动到微信并发送。 0 准备 首先需要考虑的是如何通过Python模拟鼠标操作。在此我货...
例如,假设有一个名为"script.py"的Python脚本,其中包含了一些函数或方法。 使用C语言编写代码:在C语言中调用Python脚本需要使用到Python的C API。首先需要包含相关头文件,并初始化Python解释器。然后可以使用...
面向Arcgis的python脚本编程_李明巨编著
面向ArcGIS的Python脚本编程