Python help- changing output file name if name already exists

4084
14
Jump to solution
03-24-2017 06:47 AM
TheoFaull
Occasional Contributor III

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'

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
IanMurray
Frequent Contributor

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) 


View solution in original post

14 Replies
IanMurray
Frequent Contributor

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

TheoFaull
Occasional Contributor III

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

0 Kudos
IanMurray
Frequent Contributor

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) 


TheoFaull
Occasional Contributor III

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

0 Kudos
IanMurray
Frequent Contributor

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"

TheoFaull
Occasional Contributor III

Thanks that's brill iamurray

Is there any way I can make the date format like 'Mar_27_2017'

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

... check datetime ...options ie datetime.datetime.now().strftime("%y_%m_%d_%H_%M") can all be rearranged

TheoFaull
Occasional Contributor III

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

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

patience... it is all there

d.strftime("%A %d. %B %Y")
'Monday 11. March 2002'

0 Kudos