Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 = # Message "Output workspace does not exist: "
msgCoordinateSystem =  #Message "Must Enter a Spatial Reference or Template Feature Class."
msgFail = # Message "Failed to project "

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

#Set the output workspace
output_workspace =

#Set the spatial reference
output_coordinate_system =

#Set the template dataset
template_dataset =

#Set the transformation
transformation =
#Message 86112   "Projecting multiple datasets ...""step",, 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 =
    sr = dsc.SpatialReference

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

    except Exception, ErrorDesc:
        msgWarning = msgFail + "%s" % input
        msgStr =, 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

Any help would be appreciated.

share|improve this question
up vote 5 down vote accepted

Change your block:

#Set the input datasets
inputs =
# 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").

share|improve this answer
Awesome, this worked like a charm. Thanks, so much. – Visceral May 29 '12 at 18:51
+1, much better than my lame answer :) – Chad Cooper Jun 2 '12 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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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