You are on page 1of 7

Exercise: Create a script

You have a script that includes a list of the Bismarck shapefile variables. The next step is to create
instructions for the script that will update the variables in the shapefile list to fit the standard naming
template (ND_<data>.shp) and create a new text file with the updated shapefiles.

Estimated completion time: 20 minutes

Step 1: Download the data

To complete the exercise, you need to download the data. If you have already downloaded and installed
the data, continue to the next step.

Step 2: Set up script environment

If necessary, open the BismarckShapefiles.py in PythonWin.

Note: If you did not complete the previous exercise, or you do not see
BismarckShapefiles.py in your C:\Student\PythEveryone10_1\CreatingScripts folder, open
BismarckShapefiles_temp.py.

If necessary, tile the Script and Interactive Window.

To clear the contents of your Interactive Window:


Click in the Interactive Window.
Press Ctrl + A, and then press Ctrl + X.

This will cut out all the content.

Press Enter twice to bring back the prompt >>>.


Step 2a: Set up script environment.

Step 3: Write pseudocode for the script

The script has pseudocode that describes the ingredients of the script. In this step, you will add
pseudocode that describes the instructions of the script.

The next task that this script will complete is to update the variables in the shapefile list. You will add
pseudocode for this task.

Click the script window, and then move your cursor so it is at the end of line 7.

Press Enter twice.

Type the following pseudocode:


#Update the names of the shapefiles in shapefile list

Press Enter.

Step 3a: Write pseudocode for the script.

These pseudocode steps will guide you as your write your script.

Step 4: Update the shapefiles

In this step, you will update the shapefile names in the shapefile list. In order to automate this task, you
will need to loop through each of the shapefiles in the list.

What Python statement can you use to loop through items in a sequence?

Type the following line of code on line 10:

for shp in shapeList:

Press Enter.

The line of code you just typed tells Python that you want to do something to each item (shp) in
shapeList.

Note: PythonWin automatically indents the next line of code to indicate that the following
statements, functions, and variables will loop through each of the items in the list.

Type the following code:

shp = shp.replace("nd", "ND")

Press Enter.
Step 4a: Update the shapefiles.

The indented shp.replace ("nd", "ND")method tells Python to loop through each item (shp) in
shapeListand replace any lowercase "nd" with an uppercase "ND" to fit the standard naming template
(ND_<data>.shp).

To ensure that each item has been updated, you will add printstatements to review the original and
updated shapefiles.

In the script window, click the end of line 10.

Press Enter.

Type the following code:

print shp

This will print the value for each of the original shapefile variables to the Interactive Window when you run
the script.

Move your cursor to the end of line 12.

Press Enter.

Add another printstatement for shp.

Step 4b: Update the shapefiles.


This will print the updated value for each of the shapefile variables to the Interactive Window, allowing
you to compare the original and updated values.

Click the Run button .

In the Run Script dialog box, click OK.

Step 4c: Update the shapefiles.

The for-instatement takes an item, executes the item instructions, and then moves to the next item in
the sequence. In this case, it printed the original shapefile, replaced the "nd" (if necessary), and printed
the updated shapefile.

Each of the updated shapefiles now fit the standard naming template (ND_<data>.shp).

Step 5: Create a text file

Each shapefile from shapeList has been updated. The last step is to write each of the updated shapefiles
to a text file. First, you need to create and open a text file.

In the script window, click line 8.

Press Enter.

Add the following pseudocode:

#Create and open new text file for updated shapefiles

Press Enter.

Step 5a: C reate a text file.

Type the following line of code:

path = r"C:\Student\PythEveryone10_1\CreatingScripts\BismarckUpd.txt"
Press Enter.

The variable pathhas been assigned to the path of where the text file will be created. The last part of
the path includes the name of the text file, BismarckUpd.txt.

Note: The rin front of the path indicates that this is a raw string and tells Python to
disregard any special characters, which are usually indicated with a backslash.

What function and syntax would you use to open BismarckUpd.txt in writing mode?

Open BismarckUpd.txt in writing mode, and then assign it to the variable file.

Press Enter.

file = open(path, 'w')

If you were to run the script now, a text file would be created and opened. However, you still need to
write the updated shapefiles to this text file. To do this, you will use the write method.

Click at the end of line 17 and press Enter.

Confirm that line 18 is indented.

Write the following line of code:

file.write(shp + "\n")

Step 5b: C reate a text file.

As the script loops through each item in the list, it will update the shapefile string, write the updated
shapefile to the indicated file (BismarckUpd.txt), and then start a new line for the next item (indicated
with Python's new line character: "\n").

Press Enter twice.

You are no longer inside of the for-inloop, which means that additional instructions will occur after the
script has looped through and performed the indicated tasks to each shapefile in shapeList.

Type the following line of code:


file.close()

This code will close the file once it has finished writing the updated shapefiles.

Clear the contents of your Interactive Window.

Remind me how
1. C lick in the Interactive Window.

2. Press C trl + A, and then press C trl + X.

3. Press Enter twice.

Run the script.

Remind me how

1. C lick the Run button .

2. In the Run Script dialog box, click OK.

In addition to updating the shapefiles and printing the shapefile names, your script wrote the updated
shapefiles to the BismarckUpd.txt file.

Step 6: Check script results

Once the script has successfully executed, the last step is to check the script results.

Open Windows Explorer.

Navigate to C:\Student\PythEveryone10_1\CreatingScripts.

Double-click BismarckUpd.txt.

Step 6a: C heck script results.

The script successfully wrote the updated shapefile names to the text file.

Close the .txt file, Windows Explorer, and PythonWin.

You might also like