I've seen this error pop up on questions here a few times, but I'm still running into issues with it. I'm trying to append a feature class of 'new' points to an existing layer. The main problem is that I keep getting this error:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:
From what I've seen, it seems like this is an issue with my input file not being closed, but it isn't being explicitly opened anywhere in my script.
Here is my full script:
from qgis.gui import QgsMapLayerComboBox
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLabel, QDialogButtonBox
import os
#define function that the button will run
def appendTool(self):
class AppendLayersDialog(QDialog):
def __init__(self):
super().__init__()
# Create the widgets
self.input_layer_combo = QgsMapLayerComboBox()
self.target_layer_combo = QgsMapLayerComboBox()
self.compare_field_label = QLabel("Select the field to compare for duplicates:")
self.compare_field_combo = QgsFieldComboBox()
self.compare_field_combo.setFilters(QgsFieldProxyModel.String)
# Create the layout
layout = QVBoxLayout()
layout.addWidget(QLabel("Select the input and target layers:"))
layout.addWidget(QLabel("Input layer:"))
layout.addWidget(self.input_layer_combo)
layout.addWidget(QLabel("Target layer:"))
layout.addWidget(self.target_layer_combo)
layout.addWidget(self.compare_field_label)
layout.addWidget(self.compare_field_combo)
# Add the buttons
button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
button_box.accepted.connect(self.accept)
button_box.rejected.connect(self.reject)
layout.addWidget(button_box)
# Set the layout
self.setLayout(layout)
self.setWindowTitle("Append Layers")
# Set the initial values for the layer and field combos
self.input_layer_combo.setFilters(QgsMapLayerProxyModel.VectorLayer)
self.target_layer_combo.setFilters(QgsMapLayerProxyModel.VectorLayer)
self.compare_field_combo.setLayer(self.input_layer_combo.currentLayer())
def get_input_layer(self):
return self.input_layer_combo.currentLayer()
def get_target_layer(self):
return self.target_layer_combo.currentLayer()
def get_compare_field(self):
return self.compare_field_combo.currentField()
#define function to append features
def append_features(input_layer, target_layer, compare_field):
# Create a memory layer to hold the features to be appended
mem_layer = QgsVectorLayer("Point?crs=EPSG:4326", "memory_layer", "memory")
# Add the features from the input layer to the memory layer
mem_layer.startEditing()
input_features = input_layer.getFeatures()
for feature in input_features:
# Check for duplicates in the specified field
query = QgsFeatureRequest().setFilterExpression(f"{compare_field} = '{feature[compare_field]}'")
target_features = target_layer.getFeatures(query)
if len(list(target_features)) == 0:
# Add the feature to the memory layer
mem_layer.addFeature(feature)
mem_layer.commitChanges()
# Append the features from the memory layer to the target layer
target_layer.startEditing()
target_layer.addFeatures(mem_layer.getFeatures())
target_layer.commitChanges()
# Delete the input file
#error here
**os.remove(input_layer.dataProvider().dataSourceUri())**
# Create a dialog to get the input and target layers and the field to compare for duplicates
dialog = AppendLayersDialog()
if dialog.exec_() == QDialog.Accepted:
input_layer = dialog.get_input_layer()
target_layer = dialog.get_target_layer()
compare_field = dialog.get_compare_field()
# Append features from the input layer to the target layer, checking for duplicates in the specified field
#Error here as well
**append_features(input_layer, target_layer, compare_field)**
#Make a button that will run the tool
toolbar = iface.addToolBar(u'RHD')
iface.toolButton = QToolButton()
iface.toolButton.setIcon(QIcon("AddHomes.png"))
toolbar.addWidget(iface.toolButton)
iface.toolButton.clicked.connect(appendTool)
This is the error code I'm getting:
An error has occurred while executing Python code:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'Z:/Renewal/50pointTest.shp' Traceback (most recent call last):
File "", line 86, in appendTool File "", line 75, in append_features PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'Z:/Renewal/50pointTest.shp'