Select to view content in your preferred language

Splitting the path with a variable

3448
3
01-11-2011 04:04 AM
by Anonymous User
Not applicable
Original User: klaughl

I have a script that creates a folder, a personal gdb to that newly created folder, copies a feature class and table to the gdb, performs some table work, joins the table and FC and exports it to a shapefile.

It's for a parcel process, one that we perform monthly, so the folder created is the month in which the most recent parcels will reside. So at the end of this month, we'd run the script and create a sub folder of January in the folder 2011.

In order to alleviate a lot of work to do before/after the script is run, I'm attempting to have the designation of the month folder a user-input parameter. However, after the gdb is created, the user input parameter is in the middle of the file path, which I think is creating some problems. In order for the syntax to check out, I have to leave a space before I add the '+' to allow for the variable to be placed in the middle of the path, i.e.: r"\\10.8.31.2\gisdata\Parcels\2011\ " + new_month_folder + "\MonthlyParcel.mdb\GovernmaxExtract". This makes the geoprocessor believe there's a space in the folder path name when there really isn't (it could be a work around to just add the space to the first parameter, but I don't want to hack something unstable together) and it seems like everytime I run it, I get some different obscure error reported.

So, I guess my question is, is there a better/simpler way of doing this? I have moderate python skills (mostly pertaining to ArcMap) and I'm open-minded about any suggestions.

I'll try to post as much as I can so that the situation is clearer.



Thanks in advance!

Kevin L.
Wood County, OH

==========================================================================


import arcpy
from arcpy import env

env.workspace = r"\\10.8.31.2\gisdata\Parcels\2011"


# User input for a new month folder.
new_month_folder = arcpy.GetParameterAsText(0)
outpath2 = arcpy.GetParameterAsText(1)

# New month folder variables.
new_month_folder_path = r"\\10.8.31.2\gisdata\Parcels\2011"

# New gdb variables.
gdb_location = new_month_folder
gdb_name = "MonthlyParcel.mdb"

# Copy GovernmaxExtract to new gdb variables.
in_rows = r"\\WOODMVP\D_drive\InternetExtract\InternetExtract.mdb\GovernmaxExtract"
out_table = r"\\10.8.31.2\gisdata\Parcels\2011\ " + new_month_folder + "\MonthlyParcel.mdb\GovernmaxExtract"

# Copy Polygons to new gdb variables.
in_features = r"\\10.8.31.2\Geodatabase\TaxMap.mdb\Cadastral\Polygons"
out_path = r"\\10.8.31.2\gisdata\Parcels\2011\ " + new_month_folder + "MonthlyParcel.mdb"
out_name = "Parcels"

# Add Field to GovernmaxExtract Varaibles.
in_table = r"\\10.8.31.2\gisdata\Parcels\2011\ " + new_month_folder + "MonthlyParcel.mdb\GovernmaxExtract"
field_name = "PARCEL_NO"
field_type = "text"

# Add the other field to GovernmaxExtract Variables.
in_table2 = r"\\10.8.31.2\gisdata\Parcels\2011\ " + new_month_folder + "MonthlyParcel.mdb\GovernmaxExtract"
field_name2 = "TAXID"
field_type2 = "text"

# Field calculation variables.
table_calcd = r"\\10.8.31.2\gisdata\Parcels\2011\ " + new_month_folder + "MonthlyParcel.mdb\GovernmaxExtract"
field = "PARCEL_NO"
expression = "Mid ([mpropertyNumber],5,3) & Right ( [mpropertyNumber],12 )"
expr_type = "VB"

# Other field calculation variables.
table_calcd2 = r"\\10.8.31.2\gisdata\Parcels\2011\ " + new_month_folder + "MonthlyParcel.mdb\GovernmaxExtract"
field2 = "TAXID"
expression2 = "Mid ([mpropertyNumber],1,3) & Right ([mpropertyNumber],12 )"
expr_type2 = "VB"

# Join Variables.
in_data = r"\\10.8.31.2\gisdata\Parcels\2011\ " + new_month_folder + "MonthlyParcel.mdb\Parcels"
in_field = "PARCEL_NO"
join_table = r"\\10.8.31.2\gisdata\Parcels\2011\ " + new_month_folder + "MonthlyParcel.mdb\GovernmaxExtract"
join_field = "PARCEL_NO"

# Final export varaibles
in_features2 = r"\\10.8.31.2\gisdata\Parcels\2011\ " + new_month_folder + "MonthlyParcel.mdb\Parcels"


# Geoprocessing
arcpy.CreateFolder_management(new_month_folder_path, new_month_folder)

arcpy.CreatePersonalGDB_management(gdb_location, gdb_name)

arcpy.CopyRows_management(in_rows, out_table)

arcpy.FeatureClassToFeatureClass_conversion(in_features, out_path, out_name)

arcpy.AddField_management(in_table, field_name, field_type)

arcpy.AddField_management(in_table2, field_name2, field_type2)

arcpy.CalculateField_management(table_calcd, field, expression, expr_type)

arcpy.CalculateField_management(table_calcd2, field2, expression2, expr_type2)

arcpy.JoinField_management(in_data, in_field, join_table, join_field)

arcpy.FeatureClassToFeatureClass_conversion(in_features2, out_path2, out_name2)
0 Kudos
3 Replies
by Anonymous User
Not applicable
Original User: rdharles

I'm not a big fan of using backslashes in paths.  It seems to only cause trouble.
If you do it like this it will work fine:

out_table = "10.8.31.2/gisdata/Parcels/2011/"+new_month_folder+"/MonthlyParcel.mdb/GovernmaxExtract"


Also, I noticed you don't consistently put a slash before "MonthlyParcel.mdb" in your paths.
0 Kudos
KevinLaughlin
Occasional Contributor
Thank you for the tips. Sometimes, I guess it takes a second set of eyes to find the issues.




Kevin
0 Kudos
by Anonymous User
Not applicable
Original User: curtvprice

A few tips that may make your code a little more easier to work with

1. If the workspace is set, these paths are equivalent

arcpy.env.Workspace + "/path"
"/path"

2. string substitution often makes clearer code. These are equivalent:

in_table2 = r"\\10.8.31.2\gisdata\Parcels\2011\" + \
  new_month_folder + r"MonthlyParcel.mdb\GovernmaxExtract"

in_table2 = r"\\10.8.31.2\gisdata\Parcels\2011\" + \
   r"%s\MonthlyParcel.mdb\GovernmaxExtract" % new_month_folder

3. Putting these together, with forward slashes:

arcpy.env.workspace = "//10.8.31.2/gisdata/Parcels/2011/"
...
in_table2 ="%s/MonthlyParcel.mdb/GovernmaxExtract" % new_month_folder
0 Kudos