Preamble
The first following code explains the context.
QgsProject.instance().addMapLayer(comuni)
#I call a vector in QGIS that is displayed as map that I am interested to build df7
lyr2 = QgsProject.instance().mapLayersByName('ac_002_1_v1_1_limiti_giurisdizionali_indicativi_comuni')[0]
#from the vector ac_002_1_v1_1_limiti_giurisdizionali_indicativi_comuni I create daaframe with pandas df7 that
#represents the unique values of the attribute of the field 'comune min' deprived from the disturbing character '/'
cols2 = ['comune_min']
datagen2 = ([f[col2] for col2 in cols2] for f in lyr2.getFeatures())
df5 = pd.DataFrame.from_records(data=datagen2, columns=cols2)
df6 = df5['comune_min'].str.replace('/',' ')
df7 = df6.unique()
print(df7)
#I compare df7 with df4 that has been created with the same method but should be a subset of df7. So, df8 is the difference of the two dataframe
#df8 is the dataframe that contains the string that lead to the deletion of the file. In other words,
#if the file df8 contain "comune_eccedente" have to be deleted.
df8=set(df7).difference(set(df4))
Here what happens when I print df8.
{'Neggio', 'Cerentino', 'Grancia', 'Orselina', 'Campo (Vallemaggia)', 'Bedretto', 'Agno', 'Bosco Gurin', 'Pollegio', 'Origlio', 'Personico', 'Vernate', 'Muralto', 'Ponte Capriasca', 'Breggia', 'Lavertezzo', 'Cureglia', 'Arogno', 'Cugnasco-Gerra', 'Faido', 'Porza', 'Giornico', 'Caslano', 'Riva San Vitale', 'Muzzano', 'Prato Leventina', 'Magliaso', 'Pura', 'Morcote', 'Cadenazzo Monteceneri', 'Curio', 'Paradiso', 'Lamone', 'Arbedo-Castione', 'Bodio', 'Quinto', 'Comano', 'Novaggio', 'Savosa', 'Verzasca', 'Lavizzara', 'Bedigliora', 'Bissone', 'Massagno', 'Sorengo', 'Canobbio', 'Dalpe', 'Isone', 'Acquarossa', 'Serravalle', 'Miglieglia', 'Lumino', 'Biasca', 'Gambarogno', 'Vezia', 'Linescio'}
Problem and code to be edited
Here is part of my folder "./Spedire".
Now I want to delete all files that contain in the filename a part that is an item of df8 called "comune_eccedente". If you look at the image above of the folder, the file "zp_Neggio.xlsx" and "zp_Neggio.gpkg" have to be deleted because "Neggio" is an item of the df8 and is contained in the filename. You should now focusing on the following case, which is the code that I want to edit in properlier way.
#At the moment I reconstruct the entire path
#and I delete the file based on the entire filename for specific extensions.
for comune_eccedente in df8:
try:
my_path ='./Spedire/'
file_name1 = 'zp_'+str(comune_eccedente)+'.gpkg'
complete_path = my_path+file_name1
os.remove(complete_path)
except:
pass
for comune_eccedente in df8:
try:
file_name2 = 'zp_'+str(comune_eccedente)+'.xlsx'
complete_path2 = my_path+file_name2
os.remove(complete_path2)
except:
pass
I am unsatisfied because I could simply find the match between the filename and the string "comune_eccedente" and delete ALL the files that have the match independently of their extension instead of creating specific iteration depending of the extension as I do in the code above. If in future some pdf file will be created, I have to create again specific code "try....except" for pdf and so on. That is not good.
I want a code that only look at the "comune_eccedente" item such as "Neggio" and delete all files that are inside the directory that contain "Neggio" in their filename.
df8
defined? This looks incomplete. \$\endgroup\$df8
a Pandas dataframe? If so that will suggest a very different solution from one where it's a plain iterable \$\endgroup\$df8
. Please edit the post to include more complete code, including import statements. \$\endgroup\$