Hackerszone
Welcome Guest,
learn to hack easily with tutorials, python, notepad hacks and more!
Join today, fast and free!

Are you new to hacking? Learn the basics in computer configuration, hacking tools, and hacker terminology all found here on this forum!

Join today!!

Join the forum, it's quick and easy

Hackerszone
Welcome Guest,
learn to hack easily with tutorials, python, notepad hacks and more!
Join today, fast and free!

Are you new to hacking? Learn the basics in computer configuration, hacking tools, and hacker terminology all found here on this forum!

Join today!!
Hackerszone
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Search
 
 

Display results as :
 

 


Rechercher Advanced Search

HZ Tracker
Hacking Widget Visitor Details
Latest topics
»  How to study to understand and apply RPA?
Creating a new Python script EmptyTue Feb 02, 2021 7:12 am by manas41

» SQL injection and Quote escaping
Creating a new Python script EmptySun Jun 28, 2015 11:42 am by ADS1

» [TUT] Chmod: Files & Permissions [TUT]
Creating a new Python script EmptyThu Jun 04, 2015 12:45 pm by Guest

» Reaver pixiewps
Creating a new Python script EmptyThu Jun 04, 2015 12:23 pm by voidfletcher

» How To Crash Someone's Skype in 10 SECONDS
Creating a new Python script EmptyThu Jun 04, 2015 12:20 pm by voidfletcher

» Internet Security & IP Security (IPSec)
Creating a new Python script EmptyMon May 18, 2015 9:00 pm by voidfletcher

» [Python] Infinite / Definite File Generator
Creating a new Python script EmptyMon May 18, 2015 8:58 pm by ADS1

» [C#] String Case-Inversion
Creating a new Python script EmptyMon May 18, 2015 8:57 pm by ADS1

» Rekall Memory Forensic Framework
Creating a new Python script EmptySat May 16, 2015 8:55 pm by ADS1

Who is online?
In total there are 3 users online :: 0 Registered, 0 Hidden and 3 Guests

None

[ View the whole list ]


Most users ever online was 38 on Sun Mar 19, 2023 10:07 pm

Creating a new Python script

Go down

Creating a new Python script Empty Creating a new Python script

Post by Admin Wed Apr 09, 2014 5:34 pm

To retain Python code, you create Python files (.py). These files are ASCII files that contain Python statements. The steps below assume you're using the PythonWin application, as described in Writing_Python_scripts.

Steps:

In PythonWin, click the File menu and click New. Accept the default option of Python Script and click OK.
The Script1 window opens. Script1 is the default name of your script.
Click the Maximize button on the Script1 window.
Click the File menu and click Save As. Name the script multi_clip.py and save it in a folder of your choice.
Add the following lines at the top of your script:
# Import ArcPy site-package and os modules
#
import arcpy
import os
This imports the ArcPy site-package and operating system module os to the script. The os module provides easy access to the most fundamental tools of the operating system. Some of the os module's file name manipulation methods are used in this script.

This script will have the following four arguments so it can be used generically:
An input workspace defining the set of feature classes to process
A feature class to be used by the Clip tool as the area to be clipped from an input feature class

An output workspace where the results of the Clip tool will be written
An XY tolerance that will be used by the Clip tool
Refer to the Clip tool in the Analysis toolbox for detailed information on how Clip works.

Add the following code to your script to define and set variables based on the user-defined values passed to the script at execution:


# Set the input workspace
#
arcpy.env.workspace = arcpy.GetParameterAsText(0)

# Set the clip featureclass
#
clipFeatures = arcpy.GetParameterAsText(1)

# Set the output workspace
#
outWorkspace = arcpy.GetParameterAsText(2)

# Set the XY tolerance
#
clusterTolerance = arcpy.GetParameterAsText(3)
Add the following error-handling statement and ArcPy's ListFeatureClasses() function to the script window:
try:
# Get a list of the featureclasses in the input folder
#
fcs = arcpy.ListFeatureClasses()


Python enforces indentation of code after certain statements as a construct of the language. The try statement defines the beginning of a block of code that will be handled by its associated exception handler, or except statement. All code within this block must be indented. Python uses try/except blocks to handle unexpected errors during execution. Exception handlers define what the program should do when an exception is raised by the system or by the script itself. It is good practice to use exception handling in any script using the geoprocessor so its error messages can be propagated back to the user. This also allows the script to exit gracefully and return informative messages instead of simply causing a system error.

The ListFeatureClasses() function returns a Python list of feature class names in the current workspace. The workspace defines the location of your data and where all new data will be created unless a full path is specified. The workspace has already been set to the first argument's value. A Python list is a versatile object. A for loop is used to walk through each feature class contained in the list.
Add the following code:


for fc in fcs:
# Validate the new feature class name for the output workspace.
#
featureClassName = arcpy.ValidateTableName(fc, outWorkspace)
outFeatureClass = os.path.join(outWorkspace, featureClassName)

# Clip each feature class in the list with the clip feature class.
# Do not clip the clipFeatures, it may be in the same workspace.
#
if fc <> os.path.basename(clipFeatures):
arcpy.Clip_analysis(fc, clipFeatures, outFeatureClass,
clusterTolerance)


When there are no more names in the list, the for loop ends. The ValidateTableName() function is used to ensure the output name is valid for the output workspace. Certain characters, such as periods or dashes, are not allowed in geodatabases, so this method returns a name with valid characters in place of invalid ones. It also returns a unique name so no existing data is overwritten.
The os.path's basename method is used to manipulate the clip feature class's path, so only the feature class name is evaluated in an expression, not the entire path. The Clip tool is accessed as an ArcPy function, using the various string variables as parameter values.

Add the following lines to complete the script:
except:

arcpy.AddMessage(arcpy.GetMessages(2))
print arcpy.GetMessages(2)

The except statement is required by the earlier try statement; otherwise, a syntax error occurs. If an error occurs during execution, the code within the except block is executed. Any messages with a severity value of 2, indicating an error, are added in case the script is run from a script tool. All error messages are also printed to the standard output in case the script is run outside a tool.
Add the following comments to the top of your script:


# Script Name: Clip Multiple Feature Classes
# Description: Clips one or more shapefiles
# from a folder and places the clipped
# feature classes into a geodatabase.
# Created By: Insert name here.
# Date: Insert date here.
Save the script by clicking the Save button on the PythonWin's toolbar.
The script completed above is used to learn more about using Python with Executing and debugging Python and Setting breakpoints using Python.
NoteNote:
When naming variables, remember that Python is case sensitive, so clipFeatures is not the same as ClipFeatures.

Note:

GetParameterAsText() is used to receive arguments. If a script uses defined dataset names and parameter values, it may not need to use the GetParameterAsText() function.

Note:

Statements that end with a colon indicate the beginning of indented code. Python does not use braces, brackets, or semicolons to indicate the beginning or end of a block of code. Python instead uses the indentation of the block to define its boundaries. This results in code that is easy to read and write.

Heres the completed script:

# Script Name: Clip Multiple Feature Classes
# Description: Clips one or more shapefiles
# from a folder and places the clipped
# feature classes into a geodatabase.
# Created By: Insert name here.
# Date: Insert date here.

# Import ArcPy site-package and os modules
#
import arcpy
import os

# Set the input workspace
#
arcpy.env.workspace = arcpy.GetParameterAsText(0)

# Set the clip featureclass
#
clipFeatures = arcpy.GetParameterAsText(1)

# Set the output workspace
#
outWorkspace = arcpy.GetParameterAsText(2)

# Set the XY tolerance
#
clusterTolerance = arcpy.GetParameterAsText(3)

try:
# Get a list of the featureclasses in the input folder
#
fcs = arcpy.ListFeatureClasses()

for fc in fcs:
# Validate the new feature class name for the output workspace.
#
featureClassName = arcpy.ValidateTableName(fc, outWorkspace)
outFeatureClass = os.path.join(outWorkspace, featureClassName)

# Clip each feature class in the list with the clip feature class.
# Do not clip the clipFeatures, it may be in the same workspace.
#
if fc <> os.path.basename(clipFeatures):
arcpy.Clip_analysis(fc, clipFeatures, outFeatureClass,
clusterTolerance)

except:
arcpy.AddMessage(arcpy.GetMessages(2))
print arcpy.GetMessages(2)

Admin
Coder
Coder

Posts : 101
Join date : 2014-04-07

https://thehackerszone.forumotion.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum