1

I've written some basic code which creates a new .dbf and adds two fields to it: 1 for today's date, another which should contain the names of shapefiles located in another folder. What I'd like to do is then write the names of the shapefiles to the newly created .dbf.

env.workspace = r'shapefile folder'
AT = r'location of new .dbf'
appendField = "ProjName"
dateField = "DateProc"
arcpy.AddField_management(AT, appendField, "TEXT")
arcpy.AddField_management(AT, dateField, "DATE")
shpList = arcpy.ListFeatureClasses()
for shp in shpList:
 print shp
 appendExpression = '"'+str(shp).replace('.shp','')+ '"'
 arcpy.CalculateField_management(AT, "ProjName",appendExpression, "PYTHON")
 arcpy.CalculateField_management(AT, dateField, 'time.strftime("%d/%m/%Y)', "PYTHON" )

I believe my issue is a result of having the table and feature classes in two separate workspaces. I am using 10.1. Thanks for any tips which may get this working.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jul 17, 2014 at 16:32
1
  • I should not that when I run this, it throws no errors simply doesn't the ProjName and DateProc calculate fields. Commented Jul 17, 2014 at 16:36

1 Answer 1

3

It appears that you have a few typos:

The most pythonic way to add the name of the shapefile without the extension is to split the extension from the base name, via the os module. You don't need to cast shp to a string since it already is a string.

appendExpression = os.path.splitext(shp)[0]
arcpy.CalculateField_management(AT, "ProjName",appendExpression, "PYTHON")
#You are missing a quotation mark after /%Y
arcpy.CalculateField_management(AT, dateField, 'time.strftime("%d/%m/%Y")', "PYTHON" )

Edit:

import os, time, arcpy
folder = r"C:\Users\pleblanc\Desktop\DATA"
arcpy.env.workspace = folder
AT = arcpy.CreateTable_management(folder, "AT.dbf")
appendField = "ProjName"
dateField = "DateProc"
arcpy.AddField_management(AT, appendField, "TEXT")
arcpy.AddField_management(AT, dateField, "DATE")
with arcpy.da.InsertCursor(AT, (appendField, dateField)) as cursor:
 for shp in arcpy.ListFeatureClasses():
 cursor.insertRow((os.path.splitext(shp)[0], time.strftime("%d/%m/%Y")))
answered Jul 17, 2014 at 17:05
9
  • Thanks for the suggestion using splitext, I also corrected the quotation mark issue for calculating the date. It still doesn't want to add the values to the .dbf, perhaps I need to somehow write to the .dbf as you would when exporting to excel? Commented Jul 17, 2014 at 17:15
  • @standard, Field Calculator works with DBFs. Commented Jul 17, 2014 at 17:18
  • I don't know why it won't calculate the values, the print statement I used returns the shapefile names but CalculateField doesn't seem to write them. Commented Jul 17, 2014 at 17:28
  • Drop the DBF into arcmap and try a manual field calculation from there. Commented Jul 17, 2014 at 17:31
  • Thanks I was overlooking the fact that the newly created table didn't contain any rows to be calculated. I think I'll need to use an InsertCursor, I'm not sure how eactly to go about this in the for loop. Commented Jul 17, 2014 at 17:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.