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.

  • I should not that when I run this, it throws no errors simply doesn't the ProjName and DateProc calculate fields. – standard Jul 17 '14 at 16:36
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")))
  • 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? – standard Jul 17 '14 at 17:15
  • @standard, Field Calculator works with DBFs. – Paul Jul 17 '14 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. – standard Jul 17 '14 at 17:28
  • Drop the DBF into arcmap and try a manual field calculation from there. – Paul Jul 17 '14 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. – standard Jul 17 '14 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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