0

I wrote the following piece of code that loops over a map features and buffer them. new buffer layers are saved to a different gdb with in this project.

If I run the code from IDLE while that project is not open in ArcGIS than I can run it many times and the files in the results.gdb gets overwrite with no issues. However, if I try to run the code from IDLE while that same project is open in ArcGIS and if the buffer files to be created by that code already exists (same file name) in the results.gdb (output path; destination) them I get and error that the buffer method failed:

    ERROR 000258: Output C:\\Users\\User\\Desktop\\PythonClassHWEx\\L8_HomeWore\\Ex8\\results.gdb\routes1_buffer already exists Failed to execute (Buffer).

If I try to add an IF-BLOCK to check if the file by that name already exists and then delete it before saving new buffer layer with the same name, I get an error that the schema is locked:

    Error ERROR 000464: Cannot get exclusive schema lock.  Either being edited or in use by another application or service. Failed to execute (Delete).

The code I'm trying to execute:

    import arcpy.md


#function to capture ArcGIS Pro project file elements
#and create a pre-defined buffer area around them

#init variables
arcpy.env.workspace = r"C:\\Users\\User\\Desktop\\PythonClassHWEx\\L8_HomeWore\\Ex8\\demo.gdb"
arcpy.env.overwriteOutput = True

featureclasses = arcpy.ListFeatureClasses()
bufferDistance = '850'

try:
  
  for fc in featureclasses:
    output = r"C:\\Users\\User\\Desktop\\PythonClassHWEx\\L8_HomeWore\\Ex8\\results.gdb\\"+fc+'_buffer'
    arcpy.Buffer_analysis(in_features=fc, out_feature_class=output, buffer_distance_or_field=bufferDistance+" Meters", line_side="FULL", line_end_type="ROUND", dissolve_option="NONE", dissolve_field=[], method="PLANAR")
    

  
except Exception as e:
    print("Error " + e.args[0])
    
print('Done!')

This code run just fine if using the build-in ArcGIS Pro Python window and it overwrites whatever file with same name(s) are in the output path gdb. As many time as I execute it. No errors.

5
  • Unless you are not showing the full code replace import arcpy.md with import arcpy.
    – Hornbydd
    Dec 22, 2022 at 10:10
  • 3
    What are you hoping to achieve? Do you want a way to force overwrite? Or would you be satisfied with a solution that makes it work, like providing a new name for your output? At this point it's less an ArcGIS/arcpy question and more a computing question: I have one process that has a file opened/locked, and another process attempting to do something with that file: how do I overcome the read/write locks. Usually you don't, you modify your workflow to account for it.
    – KHibma
    Dec 22, 2022 at 13:06
  • Hi @KHibma, I wrote a workaround for that issue adding epoch tine as suffix to created buffer layers (files) as they are saved in the output dgb. It is not as neat as I wonted so hence my question here. It seems there is no solution that can overwrite this lock while the file is also occupied by another program like ArcGIS. Dec 22, 2022 at 15:10
  • 3
    arcpy.env.overwriteOutput only applies to overwrite operations that are in the same process, possibly including subprocesses. The environment setting has no impact on whether a processes can overwrite a data set being used by another process.
    – bixb0012
    Dec 22, 2022 at 16:24
  • I would expect that there was an issue because the features are being read into memory, when using IDLE or CMD. Perhaps, iterate the features, add then write them to a file. Then end the feature script which would close the lock, and use the feature file to then run the buffer?
    – enolan
    Dec 23, 2022 at 14:58

1 Answer 1

1

I get similar errors when working on my code in IDLE, and haven't found a way around the schema locks. There may not be a way around it if you want to keep your code in IDLE, but if you import it as a script tool, making the following modifications, it should overwrite the data everytime, and you won't have the schema lock issue.

import arcpy.md


#function to capture ArcGIS Pro project file elements
#and create a pre-defined buffer area around them

#init variables
arcpy.env.workspace = arcpy.GetParameter(0)
arcpy.env.overwriteOutput = True

featureclasses = arcpy.ListFeatureClasses()
bufferDistance = '850'

try:
  
  for fc in featureclasses:
    output = r"C:\\Users\\User\\Desktop\\PythonClassHWEx\\L8_HomeWore\\Ex8\\results.gdb\\"+fc+'_buffer'
    arcpy.Buffer_analysis(in_features=fc, out_feature_class=output, buffer_distance_or_field=bufferDistance+" Meters", line_side="FULL", line_end_type="ROUND", dissolve_option="NONE", dissolve_field=[], method="PLANAR")
    

  
except Exception as e:
    print("Error " + e.args[0])
    
print('Done!')

With arcpy.GetParameter(0), you can turn your script into a script tool and designate the workspace where the script will run. In my experience, converting your script to a script tool will allow you to run your code while having ArcGIS open and allow for overwriting while avoiding the schema lock errors.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.