1
I created a script to delete files from a folder and its respective subfolders. These subfolders are named as follows: 1.GERAL_%i with i ranging from 1 to 137. However, I just want to delete the files from 89 folder on, I don’t want to delete folders 1 to 88. I have done the script below:
import os
my_path = os.getcwd()
foldersToSkip = list(f'1.GERAL_{i}' for i in range (1,89))
for root, dirs, files in os.walk(my_path):
if not any(x in root for x in foldersToSkip):
for file in files:
if any(y in file for y in filesToDel):
fileToDel = os.path.join(root,file)
os.remove(fileToDel)
Apparently it was supposed to work, but the code didn’t delete any file. When I debugged, I noticed the error. I noticed that the command x in root for x in foldersToSkip does not make an exact comparison between the string x and the list string foldersToSkip, but the list string was only contained in the string x. For example: script does not delete files from subfolder 1_GERAL_90 why there is a string 1_GERAL_9 on the list foldersToSkip, and when he does the comparison the output ends up being true.
My question then is, how do I change my code to make this comparison accurate?
So, I don’t want to delete the folders, but only the files inside the folders. But this will only change the final part of the code. The solution I was looking for is the function
must_be_deleted
that you presented. Thank you very much!– Carlos Eduardo
Tell me more about how you want this string comparison to work with integer here:
if groups and int(groups[1]) >= 89:
– jsbueno
@jsbueno Which string comparison with integer?
– Woss
The
shutil
was used to remove non-empty directories as it had understood that it was these that should be deleted, not just the files in them.– Woss
ah - there’s conversion there - I’ve seen regexps to deal with numbers in sequence and I’ve filled my head with "doesn’t work" before reading everything. But looking at it calmly, it’s fine.
– jsbueno
is - I was thinking here - pathlib has no equivalent to everything in shutil, and no equivalent to "os.walk" - that’s a shame. It has the "Path.iterdir" for a non-rerecursive search, and the "Path.glob" for a recursive search - but it is "Eager", not Lazy like os.walk
– jsbueno
I even thought about not using regex - the first version of the code was
name[name.rfind('_')+1:]
, but as this would make room for other name formats ending with this suffix, I ended up putting regex to simplify– Woss
@jsbueno just now saw that in the question it limits up to 137 the name of the folder and I understood what you meant. I had not seen this limitation at first, so can do without the regex well quietly even... I will edit
– Woss