4

I want to create a backup copy of a feature class and place it in a new folder with today's date written on the folder name. I also need to output file to be a shapefile.

The script below does successfully create the new folder in the style I wanted, but it fails to create the output:

ExecuteError: ERROR 000210: Cannot create output \C:\BackupData\"C:\BackupData\GM_06April2017\LITTER_BINS.shp Failed to execute (Select).

import arcpy
import os
import datetime
from arcpy import env
#Set the date to my preferred format ie: 06April2017
Cur_Date = datetime.datetime.now().strftime("%d%B%Y")
print Cur_Date
# Overwrite pre-existing files
arcpy.env.overwriteOutput = True
# Create a new folder called 'GM_' with the current date added to the folder name. Ie. GM_06April2017
newpath = r'C:\BackupData\GM_' + Cur_Date
if not os.path.exists(newpath): os.makedirs(newpath)
#Take a feature class called 'LITTER_BINS' from a File GDB and copy it to the newly created folder, but in shapefile format.
print "Starting backup process..."
env.workspace = r"C:\GIS_folder\Grounds_Maintenance.gdb"
arcpy.Select_analysis("LITTER_BINS", r"C:\Data\GM_" + newpath + "\LITTER_BINS.shp")
print "Bins"
print "Script finished"
Midavalo
30k11 gold badges53 silver badges108 bronze badges
asked Apr 6, 2017 at 9:46
6
  • Welcome to GIS:SE @TheoF! What happens if you remove r"C:\Data\GM_" from arcpy.Select_analysis() so that it reads: arcpy.Select_analysis("LITTER_BINS", newpath + "\\LITTER_BINS.shp")? Commented Apr 6, 2017 at 9:53
  • You should not combine paths like this: r"C:\Data\GM_" + newpath + "\LITTER_BINS.shp". Instead use os.path.join: os.path.join(r"C:\Data", "LITTER_BINS.shp"). Also you are trying to combine r"C:\Data\GM_" with newpath (r'C:\BackupData\GM_' + Cur_Date) Commented Apr 6, 2017 at 9:56
  • 1
    @Joseph thanks that worked a treat! I didn't need to write the whole output location at all, as I'd already defined it earlier with 'newpath' Commented Apr 6, 2017 at 10:01
  • @BERA Thanks. Though I've got the script to work fine without using os.path.join. Why is that needed? Commented Apr 6, 2017 at 10:19
  • @TheoF, you can indeed make it working without using os.path.join. The problem starts when your script grows and you need to join together multiple folders and paths and you spend hours debugging trying to find out where have you forgot to add a trailing backslash. :) os.path.join is the cleanest and safest way to join together system folders and files. Commented Apr 6, 2017 at 10:22

2 Answers 2

4

You could have used Copy Features geoprocessing tool, not the Select since you don't supply any SQL query at all. Also, you need to construct the paths using os.path.join properly. Try to print the variables before running the arcpy functions or get a PyScripter and run in Debugging mode.

import arcpy
import os
import datetime
from arcpy import env
#Set the date to my preferred format ie: 06April2017
Cur_Date = datetime.datetime.now().strftime("%d%B%Y")
print Cur_Date
# Overwrite pre-existing files
arcpy.env.overwriteOutput = True
# Create a new folder called 'GM_' with the current date added to the folder name. Ie. GM_06April2017
newpath = r'C:\GIS\Temp\BackupData\GM_{custom_date}'.format(custom_date=Cur_Date)
if not os.path.exists(newpath):
 os.makedirs(newpath)
#Take a feature class called 'LITTER_BINS' from a File GDB and copy it to the newly created folder, but in shapefile format.
print "Starting backup process..."
env.workspace = r"C:\GIS\Temp\ArcGISHomeFolder\Default.gdb"
fc_name = 'cities'
out_shp_path = os.path.join(newpath,'{fc_name}.shp'.format(fc_name=fc_name))
arcpy.CopyFeatures_management(fc_name, out_shp_path)
print "Bins"
print "Script finished"
answered Apr 6, 2017 at 10:05
1
  • Thanks @Alex_Tereshenkov. The select tool is there because I stole the script from a previous script which did use an SQL query. Although Copy Features would be better, Select works too for me. Commented Apr 6, 2017 at 10:09
1

Thanks for all the tips. Here is my final script which works a charm. Note that I didn't need to use os.path.join at all... Is it best practice to use it? Or is it not always needed? Anyway here it is (note the directories are my real life directories)

print "Script started..."
print "***"
print "This monthly script will back up all grounds maintenance feature classes as shapefiles."
print "***"
print "The backup folder is:"
print "K:\GIS2016\GIS_Data\Curo_data\Estates_data\Grounds_Maintenance\Grounds_Maintenance_Backups"
import arcpy
import os
import datetime
from arcpy import env
Cur_Date = datetime.datetime.now().strftime("%d%B%Y")
print Cur_Date
print "***"
print "Setting script to overwrite previous files..."
# Overwrite pre-existing files
arcpy.env.overwriteOutput = True
newpath = r'\\somernt\curo\GIS\GIS2016\GIS_Data\Curo_data\Estates_data\Grounds_Maintenance\Grounds_Maintenance_Backups\GM_' + Cur_Date
if not os.path.exists(newpath): os.makedirs(newpath)
print "***"
print "Starting backup process..."
env.workspace = r"\\somernt\curo\GIS\GIS2016\GIS_Data\Curo_data\Estates_data\Grounds_Maintenance\Grounds_Maintenance.gdb"
arcpy.CopyFeatures_management("LITTER_BINS", newpath + "\\LITTER_BINS.shp")
print "Bins"
arcpy.CopyFeatures_management("AMENITY_GRASS", newpath + "\\AMENITY_GRASS.shp")
print "Amenity grass"
arcpy.CopyFeatures_management("AMENITY_GRASS_SHELTERED", newpath + "\\AMENITY_GRASS_SHELTERED.shp")
print "Sheltered grass"
arcpy.CopyFeatures_management("HEDGES_1SIDE_ONLY", newpath + "\\HEDGES_1SIDE_ONLY.shp")
print "Hedges- 1 side only"
arcpy.CopyFeatures_management("HEDGES_1SIDE_TOP", newpath + "\\HEDGES_1SIDE_TOP.shp")
print "Hedges- 1 side and top"
arcpy.CopyFeatures_management("HEDGES_2SIDES_TOP", newpath + "\\HEDGES_2SIDES_TOP.shp")
print "Hedges- 2 side and top"
arcpy.CopyFeatures_management("LEAF_CLEARANCE", newpath + "\\LEAF_CLEARANCE.shp")
print "Leaf clearance"
arcpy.CopyFeatures_management("MEADOW_GRASS", newpath + "\\MEADOW_GRASS.shp")
print "Meadow grass"
arcpy.CopyFeatures_management("ROUGH_GROUND", newpath + "\\ROUGH_GROUND.shp")
print "Rough ground"
arcpy.CopyFeatures_management("SHELTERED_GRITTING_AREAS", newpath + "\\SHELTERED_GRITTING_AREAS.shp")
print "Sheltered gritting areas"
arcpy.CopyFeatures_management("SHRUBS_ORNAMENTAL", newpath + "\\SHRUBS_ORNAMENTAL.shp")
print "Shrubs"
arcpy.CopyFeatures_management("SHRUBS_WALL", newpath + "\\SHRUBS_WALL.shp")
print "Shrub wall"
arcpy.CopyFeatures_management("SOMER_GRIT_BINS", newpath + "\\SOMER_GRIT_BINS.shp")
print "Grit bins"
arcpy.CopyFeatures_management("SWEEPING", newpath + "\\SWEEPING.shp")
print "Sweeping (hard surfaces)"
print "Script finished"
answered Apr 6, 2017 at 10:27
3
  • You should use a for loop that will iterate a list of feature classes names you would like to export. You should really have a single CopyFeatures call. What if you will have a hundred of feature classes to export? You don't want to add a new line with arcpy.CopyFeatures for each of them. Commented Apr 6, 2017 at 10:33
  • @AlexTereshenkov ah... I've never tried that before. It does sound better! Can you tell me where the for command goes? Commented Apr 6, 2017 at 10:43
  • Please start new a question and it will be answered by me or someone else. Describe that you have multiple feature classes and you want to do the automation with for loop. Or you could look yourself at e-education.psu.edu/geog485/node/54. many resources on the Internet Commented Apr 6, 2017 at 10:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.