1
\$\begingroup\$

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". enter image description here

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.

\$\endgroup\$
11
  • 1
    \$\begingroup\$ Where is df8 defined? This looks incomplete. \$\endgroup\$ Jan 8, 2023 at 13:47
  • \$\begingroup\$ Is df8 a Pandas dataframe? If so that will suggest a very different solution from one where it's a plain iterable \$\endgroup\$
    – Reinderien
    Jan 8, 2023 at 13:51
  • \$\begingroup\$ df8 is a pandas dataframe! \$\endgroup\$
    – simone100
    Jan 8, 2023 at 14:06
  • 1
    \$\begingroup\$ Welcome to Code Review! Thank you for clarifying about df8. Please edit the post to include more complete code, including import statements. \$\endgroup\$ Jan 8, 2023 at 14:19
  • 1
    \$\begingroup\$ @Reinderien I have edited the post to explain better. \$\endgroup\$
    – simone100
    Jan 8, 2023 at 15:48

1 Answer 1

1
\$\begingroup\$

Don't name your dataframe variables df1, df2 etc. Give them meaningful names describing what they contain.

Don't use set() on a dataframe. Instead, use something like Index.difference.

It's good for your data to contain Italian text, but non-desirable for your variable names and schema (column names, etc.) to contain Italian text. For better or worse, English is the language of international technical collaboration. As such, comune_eccedente should be called something like surplus_municipality.

Never bare try/except, and in this case don't try at all. Use pathlib and unlink:

from pathlib import Path

municipalities = {'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'}

#At the moment I reconstruct the entire path
#and I delete the file based on the entire filename for specific extensions. 
for surplus_municipality in municipalities:
    stem = Path('Spedire') / f'zp_{surplus_municipality}'
    stem.with_suffix('.gpkg').unlink(missing_ok=True)
    stem.with_suffix('.xlsx').unlink(missing_ok=True)
\$\endgroup\$
6
  • \$\begingroup\$ ok, I see that you have answered. Thank you. A preliminar question: why I have to avoid try/except? \$\endgroup\$
    – simone100
    Jan 8, 2023 at 17:23
  • \$\begingroup\$ Logic by exception is, is general, a bad idea. And writing except with no specific exception is especially bad: it masks a whole world of exceptions that would otherwise tell you crucial failures of the program, and also prevents the proper functioning of mechanisms like Ctrl+C (the "break" signal). \$\endgroup\$
    – Reinderien
    Jan 8, 2023 at 17:25
  • \$\begingroup\$ A last question: why don't you use os.remove instead of unlink? thank you very much \$\endgroup\$
    – simone100
    Jan 8, 2023 at 17:48
  • \$\begingroup\$ os. is an older, non-object-oriented filesystem interface. pathlib is object-oriented and more easily manipulates paths. \$\endgroup\$
    – Reinderien
    Jan 8, 2023 at 17:50
  • \$\begingroup\$ how can you know if os. is younger or older than pathlib. Is there a list of "deprecating" module or a suggested good way to find out? \$\endgroup\$
    – simone100
    Jan 8, 2023 at 18:04

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.