1

I am writing a script that creates a new geodatabase, copies the feature classes from one geodatabase to this new geodatabase, and then writes the name of the feature class, the original geodatabase, the destination geodatabase, the date, and the time it was copied to a text file. I have all of that working correctly except for the time field. For some reason, the time stamp stays exactly the same despite copying the feature class over at different times. I tried nesting the time stamp within the for loop, but still they are all the same even though they are not copied instantaneously. If I leave the time stamp above the for loop, it stamps once, but if I nest it within the for loop, it still does the same thing. Has anyone encountered something like this before? I expected to have at least a few seconds between each copy.

for fc in fcs:
    arcpy.CopyFeatures_management(fc, output_geodat + "\\" + fc)
    fc_cop = str(fc)
    Hour = str(nowutc.hour)
    min = str(nowutc.minute)
    if len(str(min))<2:
       Min = "0"+str(min)
    else:
        Min = str(min)
    sec = str(nowutc.second)
    if len(str(sec))<2:
        Sec = "0"+str(sec)
    else:
        Sec = str(sec)
    Time2 = str((int(Hour)-4)) + ":" + str(Min) + ":" + str(Sec)
    print fc_cop +"," + geod + "," + output_geodat + "," + Date + "," + Time2
#Step 9: Log feature classes and date of copy.
    log("\n" + str(fc_cop) +"," + str(geod) + "," + str(output_geodat) + "," + str(Date) + "," + str(Time2))


print "Finished"
3

I imagine you've pulled a datetime you've stored as nowutc (you haven't shown this part of your code) so everytime you use it in the loop you're still just using the same value. You should be generating that value within the loop to refresh the value.

However, I do think you should use Python's datetime functions rather than constructing your own time output, for example:

import time
from datetime import datetime
fcs = [1, 2, 3, 4, 5]

    for fc in fcs:
        #arcpy.CopyFeatures_management(fc, output_geodat + "\\" + fc)
        timenow = datetime.now()
        print timenow.time().strftime('%H:%M:%S')
        time.sleep(2.7) # Pause for 2.7s each iteration to show change in time

This produces something like:

11:54:52
11:54:55
11:54:57
11:55:00
11:55:03

So putting this into your script, I think it'll look something like this:

from datetime import datetime

for fc in fcs:
    arcpy.CopyFeatures_management(fc, output_geodat + "\\" + fc)
    fc_cop = str(fc)

    Time2 = datetime.now().time().strftime('%H:%M:%S')

    print fc_cop +"," + geod + "," + output_geodat + "," + Date + "," + Time2

    #Step 9: Log feature classes and date of copy.
    log("\n" + str(fc_cop) +"," + str(geod) + "," + str(output_geodat) + "," + str(Date) + "," + str(Time2))

print "Finished"

Although you could also pull the date from the datetime and use that in a single variable instead of splitting date and time as you have.

  • I had datetime.now() instead of datetime.datetime.now(). The latter worked! (Also, I have the Date in another field.) – Smithw1 Jul 5 '17 at 19:18
0

This following Snippet adds the date of your system.

# Process: Calculate Field
arcpy.CalculateField_management(Feature_Layer, "timestamp", 
"datetime.datetime.now( )", "PYTHON", "")

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.