0

This script gets a term and a path to a folder. Its goal is then to search in every subfolder for files that contain the term "deadbolt" in it and make a list and return that list.

So far so good but at the end I want to delete the first subfolder of where the script found a deadbolt file.

So for example I do have following folder structure:

d:/Movies/
├─ Movie1/subfolder1Movie1/subfolder2Movie1/movie1.mp4.deadbolt
├─ Movie2/subfolder1Movie2/subfolder2Movie2/movie2.mpeg
├─ Movie3/subfolder1Movie3/subfolder2Movie3/movie3.avi.deadbolt

In this case I provide the path "D:\Movies" and the term "deadbolt" and want the script to return ["Movie1","Movie3"]. Because I want to delete those folder structures completely. With there subfolders and files. But how can I achieve to get the first subfolder where a file was found without regex?

import os
import re

def findDeadbolts(searchTerm,search_path):
   results = []
   for root, dir, files in os.walk(search_path, topdown=True):
      for filename in files:
         if searchTerm in filename:
            fullPath = os.path.join(root, filename)
            results.append(fullPath)
            pattern="(?<=Movies\\\\)[a-zA-Z0-9\_\-\!\?]+" #Dont wont to do it with regex since names can be qutie complex
            print(re.search(pattern,fullPath)[0])
   return results

print(findDeadbolts('deadbolt','D:\\Movies'))

1 Answers1

0

I found a solution for this. Using the method "parts" from "pathlib.Path". This gives me every part of a path. Since I know the root path I can get both lengths with len() and count the length of the root +1 or since it starts counting by 0 I just take the length of root path and this will work.

from pathlib import Path
import os
from shutil import rmtree

foundPath = "D:\Movies\Movie2\Movie2.avi.deadbolt"
rootPath = "D:\Movies"
foundParts = Path(foundPath).parts
rootParts = Path(rootPath).parts
folder = foundParts[len(rootParts)]
rmtree(os.path.join(rootPath,folder))

If there is a better solution for this comment below. ;-)