Hi guys and gals,
I have a script which exports a shapefile to a folder. The output file name is 'Properties.shp'. This file is contasntly accessed by a server machine, therefore it has a LOCK file on it.
When I run the script for a second time, it often fails because it detects that and older version of 'Properties.shp' is already sitting in the folder. So when I update this file I have to manually ammend the script to write 'Properties2.shp' as the new output file name to avoid a clash.
Any ideas of how I can get around this? The overwrite = true function won't work as a LOCK file prevents this.
What I want is for the script to rename the output file 'Properties[current_date].shp'
Solved! Go to Solution.
I would recommend setting the current date and time to a variable and concatenating it into your file name.
import datetime
import time
Cur_Date = datetime.datetime.now().strftime("%y-%m-%d-%H-%M")
print "current date and time is " + Cur_Date
Output_Shape = "Properties-" + Cur_Date + ".shp"
print "New shapefile being written will be called " + Output_Shape
arcpy.FeatureClassToFeatureClass_conversion(Sheet1_Layer, CuroProperties, Output_Shape)
You can use Arcpy.Exists to check if the dataset already exists and if it does, make the output a concatenation of the current date with the name and file type of your output.
http://stackoverflow.com/questions/311627/how-to-print-date-in-a-regular-format-in-python
Let's just say I always want the output file name to include the current date (and time), how can I ammend the output part of the code? So far I've managed to import datetime and time and print it:
import datetime
import time
print datetime.datetime.now().strftime("%y-%m-%d-%H-%M")
But what do I write in the output section?
arcpy.FeatureClassToFeatureClass_conversion(Sheet1__Layer, CuroProperties, "Properties.shp" + datetime ???????????
I would recommend setting the current date and time to a variable and concatenating it into your file name.
import datetime
import time
Cur_Date = datetime.datetime.now().strftime("%y-%m-%d-%H-%M")
print "current date and time is " + Cur_Date
Output_Shape = "Properties-" + Cur_Date + ".shp"
print "New shapefile being written will be called " + Output_Shape
arcpy.FeatureClassToFeatureClass_conversion(Sheet1_Layer, CuroProperties, Output_Shape)
I inserted your code into my code and got:
File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\conversion.py", line 1694, in FeatureClassToFeatureClass
raise e
ExecuteError: ERROR 000354: The name contains invalid characters
Failed to execute (FeatureClassToFeatureClass).
The hyphens are invalid characters for the shapefile name, replace them all with underscores and it should work fine.
Cur_Date = datetime.datetime.now().strftime("%y_%m_%d_%H_%M")
Output_Shape = "Properties_" + Cur_Date + ".shp"
... check datetime ...options ie datetime.datetime.now().strftime("%y_%m_%d_%H_%M") can all be rearranged
OK thanks. Lots of info on that page. I don't think I can set it to output a short text month format however (Jan, Feb, Mar etc.)
patience... it is all there
d.strftime("%A %d. %B %Y")
'Monday 11. March 2002'