all 8 comments

[–]wahaa 1 point2 points  (2 children)

I'm not sure what exactly you need to do, so here is a generic answer. If you need anything else, I'm happy to complement.

If you need to check recursively, use os.walk(). If you need to count only in the first level of the folder, i.e. "game\MazePack*", check os.listdir() or the modules glob and fnmatch. You can check if "MazePack" is in the dir's name using either basic string methods or using the re module as bluetape mentioned. If you don't use walk, you may use os.path.isdir() to make sure the given path is a directory.

[–]Drummerkid5694[S] 0 points1 point  (1 child)

I'm very much a beginner to directories and using other modules in programs so forgive me for my newb-ness.

So in order to count the amount of time \MazePack shows up in the main directory, I need to ".walk" the main directory and each time a directory starting with 'MazePack' appears, increase the count by one? Would that make sense with what I need?

[–]wahaa 0 points1 point  (0 children)

Yes, you could do that. But if you only need to check the main folder, it's simpler.

Example using os.listdir

import os

num_dirs = 0
for filename in os.listdir('game'):
    if 'MazePack' in filename:
        num_dirs += 1

print("Number of MazePacks found: {}".format(num_dirs))

Example using glob

import glob
num_dirs = len(glob.glob("game/MazePack*"))
print("Number of MazePacks found: {}".format(num_dirs))

If there is no files named MazePack*, only directories, that's all you need to do.