Toolman101

Pulling shapefile names from text file as variable in Python

Discussion created by Toolman101 on Feb 22, 2011
Latest reply on Feb 22, 2011 by clm42
Hello,

I am trying to write a program in python that will open a text file, read in the contents which will be the names of state shapefiles, and then merge them.  Here's the details:

I have the content of my text file, state.txt, to read exactly as follows:
"Florida.shp", "Georgia.shp", "Alabama.shp"

I need my script to read this data in as a variable to work with the arcpy merge tool.  Here's the code of my script below:

#!/usr/bin/python
import os, arcpy

myfile = open( 'state.txt', 'r')
states = myfile.read()

arcpy.env.workspace = "C:/State_Shapefiles" #Directory where state shapefiles are located.
arcpy.Merge_management([states], "C:/Merged_Shapefile")
myfile.close

states is the variable that contains the contents of the text file.  I've tried to make sure that the contents of the text file are in the correct format required by the merge tool, but I get an error stating that the files don't exist.  I can only conclude that the variable is being set properly.  Merge tool details are found at  http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000055000000.htm .

I'm new to python, so explanation on how to get it to work is great, but including the code would be even better.  Thanks for any help you can provide.

Outcomes