I think there are 2 methods which could get around the issue:
Use memory layer as input
Create a copy of your input layer as a memory layer, this way you can continue to use the same data as your original input but it also 'frees' the shapefile from being locked. The memory layer also has to be added to the QgsMapLayerRegistry before it could be used in the processing algorithm.
import glob, os, processing
input = "//input.shp"
output = "//output_100.shp"
layer = QgsVectorLayer(input,"any_name","ogr")
feats = [ feat for feat in layer.getFeatures() ]
temp = QgsVectorLayer("LineString?crs=epsg:4326", "result", "memory")
# 'temp' is the new memory layer
# Change 'LineString' to 'Point' or 'Polygon' etc depending on your layer type
temp_data = temp.dataProvider()
attr = layer.dataProvider().fields().toList()
temp_data.addAttributes(attr)
temp.updateFields()
temp_data.addFeatures(feats)
QgsMapLayerRegistry.instance().addMapLayer(temp)
# Adds memory layer with all copied attributes to ToC
interval = 100
processing.runalg("qgis:densifygeometriesgivenaninterval", temp, interval, output)
# Processing algorithm uses memory layer as input parameter
QgsMapLayerRegistry.instance().removeMapLayer(temp.id())
del layer
# Removes from ToC, deletes the dependency on original input shapefile
os.chdir("C:\Users\gfb11209\Desktop\New folder (2)//")
for input_file in glob.glob("input*"):
os.remove(input_file)
# Sets current directory to desired folder and removes "input" files
for output_file in os.listdir("."):
os.rename(output_file, output_file.replace("output_100", "input"))
# Renames "output_100" files to "input"
(Credit to @Detlev and @xunilk for their very useful answers from this post.)
Split script / restart QGIS
You could split your script into two parts. Run the first part to execute the algorithm and once the output has been saved, restart QGIS and run the second part of the script. This way, the input files should no longer be used and can be safely removed.
So your first script could look like:
import processing
input = "//input.shp"
output = "//output_100.shp"
interval = 100
processing.runalg("qgis:densifygeometriesgivenaninterval", input, interval, output)
Restart QGIS and then run:
import os, glob
os.chdir("C:/" + os.getenv('USERNAME') + "/SHP_Path//")
for input_file in glob.glob("input*"):
os.remove(input_file) # Removes all files beginning with "input" (eg. input.dbf, input.prj etc)
for output_file in os.listdir("."):
os.rename(output_file, output_file.replace("output_100", "input"))