Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
esri-arcpy-python/log_results_capture_errors_template.py at main · pmacMaps/esri-arcpy-python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
pmacMaps
esri-arcpy-python
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
esri-arcpy-python
/
log_results_capture_errors_template.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
70 lines (61 loc) · 2.59 KB
main
Breadcrumbs
esri-arcpy-python
/
log_results_capture_errors_template.py
Copy path
Top
File metadata and controls
Code
Blame
70 lines (61 loc) · 2.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# ---------------------------------------------------------------------------
# Name: Template ArcPy Script for Writing Messages to Text File
#
# Author: Patrick McKinney
#
# Created on: 01/06/2017
#
# Updated on: 9/21/2022
#
# Description: This is a template script for running ArcGIS geoprocessing tool(s).
# It is ideally suited for scripts that run as Windows scheduled tasks.
# The script writes success or error messages in a text file.
# You must update the path and name of the text file.
# ---------------------------------------------------------------------------
# Import system modules
import
arcpy
import
sys
import
time
from
datetime
import
date
from
os
import
path
# Run geoprocessing tool.
# If there is an error with the tool, it will break and run the code within the except statement
try
:
# Date the script is being run
date_today
=
date
.
today
()
# Date formatted as month-day-year (1-1-2020)
formatted_date_today
=
date_today
.
strftime
(
"%m-%d-%Y"
)
# Create text file for logging results of script
# Update file path with your parameters
# Each time the script runs, it creates a new text file with the date1 variable as part of the file name
# The example would be GeoprocessingReport_1-1-2017
log_file
=
path
.
join
(
r'C:\GIS\Results'
,
f'Geoprocessing_Report_
{
formatted_date_today
}
.txt'
)
# variable to store messages for log file. Messages written in finally statement at end of script
log_message
=
''
# get time stamp for start of processing
start_time
=
time
.
perf_counter
()
# Put ArcPy geoprocessing code here
# add message for text file
log_message
+=
'
\n
Add message about geoprocessing tool process completing
\n
'
# Get the end time of the geoprocessing tool(s)
finish_time
=
time
.
perf_counter
()
# Get the total time to run the geoprocessing tool(s)
elapsed_time
=
finish_time
-
start_time
# total time in minutes
elapsed_time_minutes
=
round
((
elapsed_time
/
60
),
2
)
# add final message to log file
log_message
+=
f"
\n
Successfully ran the geoprocessing tool in
{
elapsed_time
}
seconds on
{
formatted_date_today
}
\n
"
# If an error occurs running geoprocessing tool(s) capture error and write message
except
(
Exception
,
EnvironmentError
)
as
e
:
tbE
=
sys
.
exc_info
()[
2
]
# add the line number the error occured to the log message
log_message
+=
f"
\n
Failed at Line
{
tbE
.
tb_lineno
}
\n
"
# add the error message to the log message
log_message
+=
f"
\n
Error:
{
str
(
e
)
}
\n
"
finally
:
# write message to log file
try
:
with
open
(
log_file
,
'w'
)
as
f
:
f
.
write
(
str
(
log_message
))
except
:
pass
You can’t perform that action at this time.