I was wondering how I would modify the code of the Batch Project script shown below, to read from a text file of shapefile names as input. A sample of the textfile is shown under the code:

#Import required modules
import ConversionUtils, time

#Define message constants so they may be translated easily
msgWorkspace = ConversionUtils.gp.GetIDMessage(86109) # Message "Output workspace does not exist: "
msgCoordinateSystem = ConversionUtils.gp.GetIDMessage(86110)  #Message "Must Enter a Spatial Reference or Template Feature Class."
msgFail =  ConversionUtils.gp.GetIDMessage(86111) # Message "Failed to project "

#Set the input datasets
inputs = ConversionUtils.gp.GetParameterAsText(0)
# The list is split by semicolons ";"
inputs = ConversionUtils.SplitMultiInputs(inputs)  

#Set the output workspace
output_workspace = ConversionUtils.gp.GetParameterAsText(1)

#Set the spatial reference
output_coordinate_system = ConversionUtils.gp.GetParameterAsText(2)

#Set the template dataset
template_dataset = ConversionUtils.gp.GetParameterAsText(3)

#Set the transformation
transformation = ConversionUtils.gp.GetParameterAsText(4)
#Message 86112   "Projecting multiple datasets ..."
ConversionUtils.gp.SetProgressor("step",ConversionUtils.gp.GetIDMessage(86112), 0, len(inputs))

if (output_coordinate_system == "" or output_coordinate_system == "#") and (template_dataset == "" or template_dataset == "#"):
    raise ConversionUtils.GPError(msgCoordinateSystem) 
elif (output_coordinate_system != "") and (output_coordinate_system != "#"):
    sr = output_coordinate_system
elif (template_dataset != "") and (template_dataset != "#"):
    dsc = ConversionUtils.gp.Describe(template_dataset)
    sr = dsc.SpatialReference

for input in inputs:
        outdata = ConversionUtils.GenerateOutputName(input, output_workspace)
        #Message 86113 "Projecting "
        ConversionUtils.gp.SetProgressorLabel(ConversionUtils.gp.GetIDMessage(86113) + input)        
        ConversionUtils.gp.Project_management(input, outdata, sr, transformation) 
        #Message 86114 "Projected %s to %s successfully."
        ConversionUtils.gp.AddMessage(ConversionUtils.gp.GetIDMessage(86114) % (input, outdata))

    except Exception, ErrorDesc:
        msgWarning = msgFail + "%s" % input
        msgStr = ConversionUtils.gp.GetMessages(2)
        ConversionUtils.gp.AddWarning(ConversionUtils.ExceptionMessages(msgWarning, msgStr, ErrorDesc))



Text File Sample:

C:\Users\revresources\scratch\shapefiles\Test_Output\Geochem Merged\Pyramid_PanCon.shp C:\Users\revresources\scratch\shapefiles\Test_Output\Geochem Merged\Pyramid_Rocks.shp C:\Users\revresources\scratch\shapefiles\Test_Output\Geochem Merged\Pyramid_Silts.shp C:\Users\revresources\scratch\shapefiles\Test_Output\Geochem Merged\Pyramid_Soils.shp C:\Users\revresources\scratch\shapefiles\Test_Output\test2\mod\interpretive alteration_point.shp C:\Users\revresources\scratch\shapefiles\Test_Output\test2\mod\interpretive alteration_polyline.shp C:\Users\revresources\scratch\shapefiles\Test_Output\test2\mod\Interpretive Pyramid geology_polyline.shp C:\Users\revresources\scratch\shapefiles\Test_Output\test2\mod\Interpretive Pyramid geology_region.shp

2 Answers 2


Change your block:

#Set the input datasets
inputs = ConversionUtils.gp.GetParameterAsText(0)
# The list is split by semicolons ";"
inputs = ConversionUtils.SplitMultiInputs(inputs)


inputs = [item.replace('\n', '') for item in open(filename).readlines()]

Where filename is your file with the list of SHP ("Text File Sample").

  • Awesome, this worked like a charm. Thanks, so much.
    – Visceral
    May 29, 2012 at 18:51
  • +1, much better than my lame answer :) Jun 2, 2012 at 12:43

Open up the file, read it into memory, iterate through each line, and do something with the input. I put your text file sample into a file called inputs.txt:

Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open("inputs.txt").readlines()
>>> for line in f:
...     print line
C:\Users\revresources\scratch\shapefiles\Test_Output\Geochem Merged\Pyramid_PanC
on.shp C:\Users\revresources\scratch\shapefiles\Test_Output\Geochem Merged\Pyram

C:\Users\revresources\scratch\shapefiles\Test_Output\Geochem Merged\Pyramid_Silt
s.shp C:\Users\revresources\scratch\shapefiles\Test_Output\Geochem Merged\Pyrami

C:\Users\revresources\scratch\shapefiles\Test_Output\test2\mod\interpretive alte
ration_point.shp C:\Users\revresources\scratch\shapefiles\Test_Output\test2\mod\
interpretive alteration_polyline.shp

C:\Users\revresources\scratch\shapefiles\Test_Output\test2\mod\Interpretive Pyra
mid geology_polyline.shp


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.