Hello
I am getting error to run this code with Python 2.7
Error invalid characters at this part
f"{os.path.splitext(txtFileNameWithExt)[0]}_{datetime.datetime.now():%Y_%m_%d}"
how can I fix this it? I need to run it on 2.7 .
#rename shapefile
txtFileNameWithExt = "Ops.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")
Solved! Go to Solution.
yep, need to format the date there...
#rename shapefile
txtFileNameWithExt = "Ops.shp"
dte = datetime.datetime.now()
txtFileNameNoExt = "{}_{}".format(os.path.splitext(txtFileNameWithExt)[0], dte.strftime('%Y_%m_%d'))
arcpy.env.workspace = my_Path
arcpy.Rename_management(txtFileNameWithExt,"{}.shp".format(txtFileNameNoExt))
f decorators are Python 3. You'll have to use .format() like this:
#rename shapefile
txtFileNameWithExt = "Ops.shp"
txtFileNameNoExt = "{}_{}".format(os.path.splitext(txtFileNameWithExt)[0], datetime.datetime.now(%Y_%m_%d))
arcpy.env.workspace = my_Path
arcpy.Rename_management(txtFileNameWithExt,"{}.shp".format(txtFileNameNoExt))
Thanks for replay
I am getting error using your code
File "C:\Sch\osp.py", line 74
txtFileNameNoExt = "{}_{}".format(os.path.splitext(txtFileNameWithExt)[0], datetime.datetime.now(%y_%m_%d))
^
SyntaxError: invalid syntax
yep, need to format the date there...
#rename shapefile
txtFileNameWithExt = "Ops.shp"
dte = datetime.datetime.now()
txtFileNameNoExt = "{}_{}".format(os.path.splitext(txtFileNameWithExt)[0], dte.strftime('%Y_%m_%d'))
arcpy.env.workspace = my_Path
arcpy.Rename_management(txtFileNameWithExt,"{}.shp".format(txtFileNameNoExt))