Only Enumerate Files In Folders With Specific Names
Solution 1:
You're confusing root
and dirs
. root
is the "current directory" at each level; dirs
is a list of directories visible at this level.
Your current code processes all files in each directory, once per visible subdirectory. What you want is to see whether your current directory is inbox
or sent
, and only then do your processing.
for root, dirs, files inos.walk(top_level_folder):
if root.lower().endswith("inbox") or root.lower().endswith("sent"):
for fn in files:
with open(os.path.join(root, fn), 'r') as f:
pass # do something
You can also set topdown=True
in your walk
call, and then modify which subdirectories you want to walk into.
for root, dirs, files in os.walk(top_level_folder, topdown=True):
if root != top_level_folder:
# only recurse into level 3+ directories with the desired names
dirs[:] = [d for d in dirs if d in ['inbox', 'sent']]
if root.lower().endswith("inbox") or root.lower().endswith("sent"):
for fn in files:
withopen(os.path.join(root, fn), 'r') as f:
pass# do something
However, I find that option to be a little bit ugly (especially since you need a special case at the top level to avoid skipping /doe-john
, etc.). In your particular case, since there are only two directories you want to look at and they're only one level down, I wouldn't use walk
at all:
for person inos.listdir(top_level_folder):
inbox = os.path.join(top_level_folder, person, 'inbox')
sent = os.path.join(top_level_folder, person, 'sent')
for file inos.listdir(inbox):
pass # Do something
for file inos.listdir(sent):
pass # Do something
Solution 2:
You can modify the dirs
returned from os.walk()
if you use the topdown=True
option. According to the documentation:
When topdown is
True
, the caller can modify the dirnames list in-place (perhaps usingdel
or slice assignment), andwalk()
will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to informwalk()
about directories the caller creates or renames before it resumeswalk()
again. Modifying dirnames when topdown isFalse
is ineffective, because in bottom-up mode the directories in dirnames are generated before dirpath itself is generated.
Post a Comment for "Only Enumerate Files In Folders With Specific Names"