You combine whereClause3 and whereClause4 with the AND operator.
As gm70560 wrote, the variables workforceField, workforceindex are not defined. That's why, first you have to define them. I guess workforceindex is a double or at least a number datatype like farmfieldindex. So you have to Change:
whereClause3 = "\"%s\" = '%s'" % (workforceField, workforceindex)
to:
whereClause3 = "\"%s\" = %s" % (workforceField, workforceindex)
The whereClause4 is not defined (commented out). You have to change
whereClause = "\"%s\" = '%s'" % (farmField, farmfieldindex)
to:
whereClause4 = "\"%s\" = %s" % (farmField, farmfieldindex)
Is that right? (I removed the single quotes for the farmfieldindex)
Another reason for getting an error should be the following:
whereClause3 + "AND" + whereClause4
Is the same like: "\"%s\" = %s" + "AND" + "\"%s\" = %s"
Is the same like: "\"%s\" = %sAND\"%s\" = %s"
Thus, you have no blank between the value (workforceindex) of the first part of the where clause and the AND Operator. Change the Select Layer by Attribute Function to the following and it should work:
arcpy.SelectLayerByAttribute_management(countiesL, "NEW_SELECTION", whereClause3 + " AND " + whereClause4)
If it still not working check the delimiters in the where clause (have a look at the AddFieldDelimiters function)
As I mentioned in the comments, ListFields
on an invalid data source is the probable cause. There are several other issues that are going to trip you up once you solve that though. I took a rough pass at the code
import arcpy
import csv
table = r'your_input'
output = r'your_output'
fields = arcpy.ListFields(table)
field_names = [field.name for field in fields if field.type != 'Geometry']
with open(output, 'w') as f:
w = csv.writer(f)
w.writerow(field_names)
for row in arcpy.SearchCursor(table):
field_vals = [row.getValue(field) for field in field_names]
w.writerow(field_vals)
del row
If you're using at least 10.1, you'd be better off using arcpy.da.SearchCursor
.
import arcpy
import csv
table = r'your_input'
output = r'your_output'
fields = arcpy.ListFields(table)
field_names = [field.name for field in fields if field.type != 'Geometry']
with open(output, 'w') as f:
w = csv.writer(f)
w.writerow(field_names)
with arcpy.da.SearchCursor(table, field_names) as cursor:
for row in cursor:
w.writerow(row)
Best Answer
You need to check if the file already exists using
arcpy.Exists(output_path)
and ignore the shapefile:Alternatively, you could also use
os.path.exists(output_path)
. There are some other things I would like to point out:ff
would be a file name including extension. Assumingff
islakes.shp
, then'{ff}.shp'.format(ff=ff)
would evaluate tolakes.shp.shp
.arcpy.env.overwriteOutput = False
to make sure that files are never overwritten. An error would occur in case there is a bug in the script to prevent accidental overwritten files.enumerate
(as you see in my examples) instead of two loopsHere is an example using
arcpy.Exists(output_path)
along with the other suggested changes:Alternatively, you can also change the file name of the duplicate file:
Note: Your solution, and both of my examples, takes the first n files (equals to
max_number
) from the result list ofarcpy.ListFeatureClasses("*.shp")
.Unfortunately, the documentation does not specify the order of the list. From all we know this could be completely random.
You could use following code instead of
files = arcpy.ListFeatureClasses("*.shp")
to ensure that the file order is always the same:However,
files
' elements arePath
objects. You can access the file name withfile.name
(assumingfile
is aPath
object). Here an example: