7

As I understand it, overwriteOutput is an environment setting that controls whether an output overwrites an existing file of a duplicate name. Just re-checked the help pages, which confirms.

Despite having checked the relevant box in Geoprocessing > Geoprocessing Settings, as well as specified in my standalone python script that overwriteOutput = True, I received an error that the output file already exists.

bathy = r'F:\Joe_School\Thesis\data\kur_5m_bathy\kure_5m'
out_prof = r'F:\Joe_School\Thesis\scripts\Jerry\scratch.gdb\testprof'

import arcpy, os, math, time
from arcpy import env
import arcpy.management as DM
arcpy.CheckOutExtension("spatial")

env.overwriteOutput = True
desc = arcpy.Describe(bathy)
env.outputCoordinateSystem = desc.spatialReference
env.workspace = os.path.dirname(out_prof)

centroid = pointx, pointy = (367521.402, 3147853.81)
XMin = pointx - 200
XMax = pointx + 200
YMin = pointy - 200
YMax = pointy + 200
patch = SA.ExtractByRectangle(bathy, SA.Extent(XMin, YMin, XMax, YMax), "INSIDE")

DM.CreateFeatureclass(os.path.dirname(out_prof), os.path.basename(out_prof), "POLYLINE", "", "", "", patch)

It errors out on CreateFeatureClass.

Message File Name   Line    Position    
Traceback               
    <module>    <module1>   22      
    CreateFeatureclass  c:\program files (x86)\arcgis\desktop10.3\ArcPy\arcpy\management.py 1807        
ExecuteError: ERROR 000258: Output F:\Joe_School\Thesis\scripts\Jerry\scratch.gdb\testprof already exists
Failed to execute (CreateFeatureclass).

Given that there are 2 places that I know of to set this env setting, which takes precedent, and how do I make my script overwrite duplicate files?

I looked at arcpy.env.overwriteOutput = True appears not to be working?, but the problem there seemed to be with an associated function, and not the 'overwriteOutput' setting.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
joechoj
  • 443
  • 3
  • 15
  • What you describe in words (excluding the code you have pasted) should work. I cannot comment on your code because you have only presented something that appears to be on its way to becoming a code snippet that works up to where you are stuck. I say this because you have a suspicious ... in the middle of it and you seem to be using some aliases DM and DA that not only make the code harder to read but have not been specified. – PolyGeo Sep 09 '16 at 23:38
  • Also, if the issue is with the CreateFeatureClass line then anything after that is irrelevant to the question that you are asking. To be certain that it is the CreateFeatureClass line at fault can you include the full error message, which includes its line number, from running any code snippet that you present, please? – PolyGeo Sep 09 '16 at 23:41
  • @PolyGeo: Sure. Anything I've left out is an attempt to make things more legible. I've gone back and added the DM and DA assignments, as well as the full error message. The line with CreateFeatureClass is the line identified in the error message. There are 16 more lines I can add in place of '...', but I'm pretty sure they're irrelevant. I'll go ahead and add them, and I suppose I can remove them again later for clarity once people have ruled them out as the problem. – joechoj Sep 09 '16 at 23:47
  • I recommend reviewing http://meta.gis.stackexchange.com/questions/4312/writing-code-snippets-to-get-quicker-answers, and to focus on trying to create code snippets that contain the minimum code for anyone to see the same problem you do. – PolyGeo Sep 09 '16 at 23:50
  • Yes, I was trying to be concise, but you'd requested more. I'll review the doc and re-condense. – joechoj Sep 09 '16 at 23:54
  • 1
    See http://gis.stackexchange.com/a/34964/64785 – Midavalo Sep 10 '16 at 01:53
  • @PolyGeo: I'm pretty dismayed to see the reputation I've lost after the changes I made to attempt to satisfy your requests. Can't voting be frozen when a topic is on hold? I hope you can appreciate I find this community valuable, and I'm trying to raise my rep to 50 so I can ask follow-ups to interesting topics. I'm happy to admit I'm new, I'm not experienced, and have a lot to learn. But this process is pretty discouraging. I don't know how I can accrue rep and participate if I'm not allowed to comment elsewhere, and my own first attempts at questions get downvoted because I'm still learning. – joechoj Sep 11 '16 at 17:04
  • I think you will find the site much easier to use once you have taken its 2-minute [Tour] which is designed to introduce all users to its protocols such as only one question per question. Follow up questions should be asked as questions not comments. Comments are only there to help posters improve their own posts. You can always comment on your own posts. – PolyGeo Sep 11 '16 at 20:40
  • 1
    For ideas on how to quickly accrue the 50 pts of rep and why we require it to comment everywhere please review http://meta.gis.stackexchange.com/q/3860/115. It looks like you got downvotes when you appeared to be just dumping your code in (which is not what I requested) rather than working to write a code snippet that would help us help you. . – PolyGeo Sep 11 '16 at 20:44
  • There is actually a way to freeze voting while you are improving a question. That is to delete it, work on it and then undelete it. However, deleting too many questions can lead into automated question limits and ban algorithms, and causes you to miss out on feedback as you work up your question so I would not recommend that strategy. Your questions, and rep, will improve fast if you carefully assess advice offered in comments. – PolyGeo Sep 11 '16 at 20:53
  • @PolyGeo: Thanks for the feedback. There may have been a misunderstanding from the sequence of our exchange. After I first posted and saw your initial comment, I tried to follow your suggestion by adding more lines of code and the full error message to give better context. When you responded that it still wasn't enough because you couldn't reference the line # from the error message, that's when I posted the whole script. I know better now after reading the references you suggest, but I thought that's what you were requesting at the time. At any rate, thanks for the guidance. I'll keep trying. – joechoj Sep 12 '16 at 00:57
  • That's no problem, we have a lot of new users each day, and it is hard to tailor advice to each, so we do sometimes have to just rely on more swimming than sinking as we try to bring them up to speed on focussed Q&A as quickly as we can. – PolyGeo Sep 12 '16 at 01:02

1 Answers1

8

You said this is a standalone script, so it is possible that there is already a lock on the feature class you are trying to overwrite if you have it open in an MXD already. From the ArcMap resource on File Geodatabases and Locking regarding editing or modifying a dataset::

However, if another process is already accessing the data, whether it be a process on your computer or another user's, ArcGIS prevents you from performing any of these operations until the other process has terminated.

It does this by placing a .lock file on the feature class. If you have that feature class loaded in an MXD that is currently open and you attempt to overwrite it using a tool within ArcMap, the tool will look to the environment settings and if overWrite = True then it will attempt to overwrite it. If that feature class is only locked by that one particular process that is attempting to modify it, it will be permitted to overwrite.

However, if that feature class is also locked by another process (eg open in another MXD concurrently, editing its metadata in ArcCatalog, or being accessed by a standalone script tool), then regardless of the environment setting for overwrite the feature class cannot be modified until the other locks are removed. When the process accessing the feature class is terminated, the lock is removed (or supposed to - sometimes you can end up with .lock files that remain when they shouldn't).

Make sure that any other processes that might be accessing the dataset are terminated. Try the following:

  • close any MXDs that are accessing the feature class

  • reboot your python editor

  • sometimes even rebooting your machine and ArcMap/ArcCatalog fully can do wonders for removing pesky lingering lock files that won't disappear.

For the query regarding which level of the overwrite environment setting takes precedent - I would read over the documentation for geoprocessing enviroment settings and hierarchy, and also the related subpage on script environments and submit a new question if you need to. If you are running a standalone script, the overwrite environment setting is not inherited from the application level settings (what you set in Geoprocessing options in ArcMap) - the only thing that matters is what is set in the script.

malcoholic
  • 142
  • 6
  • This looks promising. I've been able to run it successfully after closing out the MXD, and the overwrite does occur. So overwrite will only occur if enabled in ArcMap and running native tools; or using ArcPy with layer not loaded in an MXD, yes? I made the following edits to your explanation, for clarity. Please correct me if I've misinterpreted. 1: "The overwrite environment setting ..." --> "The overwrite environment setting within ArcMap...." 2: "It only works like that" --> "Overwriting will only occur". 3: "... any other MXDs" --> "... any other open MXDs". – joechoj Sep 12 '16 at 22:17
  • 1
    @joechoj It sounds like that was the issue then! I have edited my answer fairly extensively to try to explain it more clearly and comprehensively. – malcoholic Sep 13 '16 at 01:03
  • I think that explains it. Thanks very much for expanding your answer, very helpful. – joechoj Sep 13 '16 at 04:00