Professional Documents
Culture Documents
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.
To complete the exercise, you need to download the data. If you have already downloaded and installed
the data, continue to the next step.
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.
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.
These pseudocode steps will guide you as your write your script.
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?
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.
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.
Press Enter.
print shp
This will print the value for each of the original shapefile variables to the Interactive Window when you run
the script.
Press Enter.
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).
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.
Press Enter.
Press Enter.
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.
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.
file.write(shp + "\n")
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").
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.
This code will close the file once it has finished writing the updated shapefiles.
Remind me how
1. C lick in the Interactive Window.
Remind me how
In addition to updating the shapefiles and printing the shapefile names, your script wrote the updated
shapefiles to the BismarckUpd.txt file.
Once the script has successfully executed, the last step is to check the script results.
Navigate to C:\Student\PythEveryone10_1\CreatingScripts.
Double-click BismarckUpd.txt.
The script successfully wrote the updated shapefile names to the text file.