Anyone know how to make python put a date stamp on a filename when creating a feature?
Here is how I had to code it:
from datetime import Date
#Sets a variable to hold today's date
currentdate = today()
Process: Feature Class to Feature Class (6)
arcpy.FeatureClassToFeatureClass_conversion(in, out loc, "output_"+str(currentdate)
The result is: output_2014-10-23
The “-“ causes failure as an invalid character
Can you clarify your question a little? Are you looking to put a date stamp on a new feature class? I've written a few scripts that utilize dates and times and the method changes a little based on where you want to put the data. Let me know and I can try to help.
Yes on a new feature class. I am running a feature to feature script that filters specific data for each feature and I need to date stamp them so that I can view changes in a side by side comparison and the output goes into a File Geodatabase.
And you would like the date stamp to be on the feature class? So you could check today's "River" export against yesterday's "River" feature class? If I'm understanding you right then I would suggest importing datetime. Depending on how you want to format the date you have some options shown here 8.1. datetime — Basic date and time types — Python 2.7.8 documentation
#Set a variable to hold today's date
currentdate = datetime.date.today()
#Execute the feature to feature function. Casting as a string just to be safe
arcpy.FeatureClassToFeatureClass_conversion(in_features, out_path, out_name + str(currentdate))
Yes, that is basically what I am trying to do, I will give that a try and let you know how it goes.
Thanks for your quick responses,
You're welcome and good luck. If it gives you trouble let me know and I can try to help.
Good morning Brandon,
I figured out how to send the string to the feature output but the format is 2014-10-23 and I need it to be 20141023. I have read through documentation and have not been able to figure out how to do this format could you please assist?
I'm not sure how you ended up coding it but you can modify your date object with .strftime which lets you tell the date objects how to orient themselves. There is a really good reply on stack overflow datetime - How to print date in a regular format in Python? - Stack Overflow
But in your case I would say code like currentdate.strftime(%Y%b%d) should format it the way you want. You can reference the formatting letters in either the link or the help for strftime.
Understood. Try this
nohyphendate = currentdate.strftime(%Y%m%d)
print "output_" + nohyphendate
I had an error in my previous reply. If you use a %b you get the month abbreviation. %m gets you the integer of the month. Does that fix your error?
Here is what I used and it worked:
currentdate = datetime.date.today().strftime("%Y""%m""%d")
Figured it out. Thanks for your help
You're welcome. Glad you were able to make it work.
Retrieving data ...