How add current date to output when I convert feature class to shapefile?

1378
7
02-11-2022 01:44 PM
anonymous55
Occasional Contributor II

Hello 
I am trying to rename shapefile to current date like this dd_02112022.zip
I am created zip file but I can't find out how to rename shapefile or feature class before zipping all together.

any python code or arcpy to rename and add current date.

Best,

0 Kudos
7 Replies
DanPatterson
MVP Esteemed Contributor

many ways to generate the date string, here is one.  I assume you have the rest in hand

from datetime import datetime as dt

n = dt.now()

"-".join([f"{i}" for i in [n.year, n.month, n.day]])
'2022-2-11'

 


... sort of retired...
KimGarbade
Occasional Contributor III

This worked for me using the rename_management function and a shapefile.  It took a shapefile name Kim.shp and renamed it to append todays date... the code to create the zip file works to, but it creates a zip file that starts at the base of the path... if that makes sense.

KimGarbade_3-1644775008572.png

 

KimGarbade_4-1644775081222.png

 

 

 

anonymous55
Occasional Contributor II

Hello @KimGarbade 

I don't way but I run your code on jupyter notebook on Arcpro and works fine when I am trying to run on PyScripter I am getting error 

ARM_1-1644857821387.png

#rename shapefile
txtFileNameWithExt = "op.shp"
txtFileNameNoExt = f"{os.path.splitext(txtFileNameWithExt)[0]}_{datetime.datetime.now():%Y_%m_%d}"
arcpy.env.workspace = my_Path
arcpy.Rename_management(txtFileNameWithExt,f"{txtFileNameNoExt}.shp")

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

you are missing a matching  }


... sort of retired...
0 Kudos
anonymous55
Occasional Contributor II

I checked and I don't miss anything
seems "f" doesn't work here 

ARM_0-1644858724264.png

txtFileNameNoExt = f"{os.path.splitext(txtFileNameWithExt)[0]}_{datetime.datetime.now():%Y_%m_%d}"
^
SyntaxError: invalid syntax

also here

arcpy.env.workspace = my_Path
arcpy.Rename_management(txtFileNameWithExt,f"{txtFileNameNoExt}.shp")

 

0 Kudos
KimGarbade
Occasional Contributor III

"f" substitution only works in Python 3.6 and above.... might have to use an older substitution method.  You could try % formatting... its an older way to do string substitution in Python.

0 Kudos
KimGarbade
Occasional Contributor III

I wrote mine in Spyder... I don't know much about PyScripter, but maybe it needs a reference to the 'os' or 'datetime' modules

0 Kudos