I want to go through a hirarchy-tree and check if I am at a specific directory (see picture). If this is true, I want to append this path (including the filenames in this path) to an array. I got stuck at the part where I want to check the current directory. Here is my code so far:
PATH = "../../data/"
paths = []
for path, dir, filenames in os.walk(PATH):
if dir == "root/dir2/1":
for filename in filenames:
paths.append(os.path.join(path, filename))
break
diris a built-in Python keyword and should not be used as a variable name.diris a list of subdirectory names -dirnamesmight be a better name for it. It will never be equal to any string.pathis what tells you what directory is currently being walked, but I'm pretty sure it will never be equal to your string (it will be whatever path you passed toos.walk(), with additional directories added onto the end). Note that if you're only interested in files in a single directory, you should just useos.listdir()on that directory;os.walk()is inappropriate.glob.glob('**/1', recursive=True)?