2
I am very beginner and I have a doubt in a situation that I am creating, if anyone can help me thank you.
I am trying to develop a program in Python that solves my daily stress of deleting some files in folders. I have to check the files by name in this format:
- 01 - Carlos
- 01 - TSE
- 02 - John
- 02 - TSE
- 03 - TSE
- 04 - Mary
- 04 - TSE
Deleting the "TSE" that has number equal to names and leaving the "TSE" that has no other aruivo with same number, the list would be like this:
- 01 - Carlos
- 02 - John
- 03 - TSE
- 04 - Mary
I can even delete all the files that have TSE in their name, but I should leave those that don’t match with numbered names. My code is like this:
from time import sleep
import os
import glob
pasta = os.getcwd()
tipo = '*.txt'
texto = 'TSE'
arquivos = glob.glob1(pasta, tipo)
n = 1
y = 1
ok = int(input(f'''
<====>\033[1;31mOS arquivos "{tipo}" com "{texto}" em seus nomes serão DELETADOS!\033[m<====>
\n
\033[0;33m[1] OK
[2] CANCELAR\033[m \n
Opção: '''))
if ok == 1:
for x in arquivos:
txnum = str(n).zfill(2)
if texto and txnum in x:
os.remove(arq)
print(f'Arquivo "{x}" DELETADO!!')
print('=' * 20)
n += 1
print('Finalizando o Programa!!')
sleep (3)
exit()
I would suggest that first create an array with all the filenames in the folder, then iterate that array and check if that file ends in TSE and has any item in the list that starts by the file’s Numer, skipping, of course, the iteration item
– Costamilam