I have a feature class table that i need to export to excel, the feature class will be created weekly. The problem i am having is that on creation the feature class gets the date added to the end of the feature class name and i am trying to access with a wildcard because the date will change based on when the feature class gets created. How can i get the feature class with a wildcard?
import arcpy workspace = "Database Servers\GIS.gds\GISSERVER1 (VERSION:dbo.DEFAULT)" arcpy.env.workspace = workspace ''' lyrs = ['Hyrdo_FINAL_ALL_*'] for fc in arcpy.ListFeatureClasses(): for lyr in lyrs: if fc.startswith(lyr): print 'lyr' ''' lyr = "Hyrdo_FINAL_ALL_*" #Database Servers\GIS.gds\GISSERVER1 (VERSION:dbo.DEFAULT)\Hyrdo_FINAL_ALL_121817 arcpy.MakeFeatureLayer_management(lyr, "In_memory\lyr1") arcpy.TableSelect_analysis("In_memory\lyr1","C:/Temp/Export_Output.xls", "") print 'done'
ListFeatureClasses has an optional wildcard parameter I think you could use.
arcpy.env.workspace = r"[path to your workspace]" lyr = arcpy.ListFeatureClasses("Hyrdo_FINAL_ALL_*") # do something with lyr
By the way, I don't think an excel file is a valid output to TableSelect. If you just want to write the table to an excel spreadsheet you could do:
Hope this helps!
Hmmm, it's possible that would due to a typo. Is the spelling "Hyrdo_FINAL_ALL_..." or "Hydro_FINAL_ALL_..."?
Also, is the old feature class deleted and replaced by the date-stamped new copy, or is a date-stamped new copy added to the workspace? If there is a stack of them going back multiple weeks you might need to parse the date field like Randy said to find the newest one.
First, I would use a date format that is suitable for sorting: YYMMDD. Append this to your feature names when backing up. You can then append the feature name to a list (if it starts with, contains, etc. what you are looking for) when you loop through a ListFeatureClasses or ListLayers, sort the list and then pick the first or last item in the sorted list as the oldest or newest.
features = ['Hyrdo_FINAL_ALL_171218','Hyrdo_FINAL_ALL_171012', 'My_Feature_170101', 'Another_Feature_161012'] found =  for feature in features: if feature.startswith('Hyrdo_FINAL_ALL_'): # assumes this is name up to date found.append(feature) found.sort() print found[-1:]
that error message is only returned when there is an empty list...
a =  # define an empty list a # slice the empty list and get an error Traceback (most recent call last): File "<ipython-input-18-6a1284577a36>", line 1, in <module> a IndexError: list index out of range
Now you say... but the list isn't empty...
Another way to get the error
a = [1,2] # so now we have 2 elements in the list... a # so let's get the second Traceback (most recent call last): File "<ipython-input-21-fc907be37984>", line 1, in <module> a IndexError: list index out of range
But python is zero-indexed, so if you want the second, then you have to slice by , since  returns the first.
That's how slicing works... it is either empty or you sliced beyond the range, or you sliced wrong.