Select to view content in your preferred language

python error for f"{os.path

466
3
Jump to solution
03-24-2023 09:54 AM
anonymous55
Occasional Contributor II

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")

 

 

 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

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))

View solution in original post

3 Replies
by Anonymous User
Not applicable

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))

 

anonymous55
Occasional Contributor II

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

0 Kudos
by Anonymous User
Not applicable

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))