brb
brb

Reputation: 1179

find files in subdirectories only with python

I have a parent folder (PF) with child folders (CF1, CF2, CF3...) and files both in the parent folder (PFf1.csv, PFf2.csv,...) and children folders (CF1f1.csv, CF1f2.csv, CF2f1.csv, CF2f2.csv,...)

I want to find only the files in the children folders (CF1f1.csv, CF1f2.csv, CF2f1.csv, CF2f2.csv,...) and ignore the files in the parent folder.

All the examples I have seen in stackoverflow and the internet are of the form:

for folder, subfolders, files in os.walk(rootDir):
   for f in files:
      print(f)

which finds the files in the parent folder also. I have tried a to modify this to:

but without success. I feel this should be easy, but I am new to python and cannot figure it out. Any help would be much appreciated.

Upvotes: 2

Views: 3126

Answers (3)

Ahmad Khan
Ahmad Khan

Reputation: 2703

You can first get all of the sub directories in the root directory using os.listdir and putting a check with os.path.isdir:

>>> from os import listdir
>>> from os.path import isfile, isdir, join

>>> root_dir = './PF'
>>> sub_dirs = [join(root_dir, dir) for dir in listdir(root_dir) if isdir(join(root_dir, dir))]
>>> sub_dirs
['./PF/CF2', './PF/CF1']

And then iterate over all the sub-directories again using os.listdir to get files in them. You can use os.path.isfile to check for only files:

>>> sub_dir_files = [f for subdir in sub_dirs for f in listdir(subdir) if isfile(join(subdir, f))]
>>> sub_dir_files
['CF2f2.txt', 'CF2f1.txt', 'CF1f2.txt', 'CF1f1.txt']

Upvotes: 1

KGS
KGS

Reputation: 685

your loop gives you the path to the current folder, you can check if it is different than the rootDir (provided rootDir is a full path ofcourse):

for folder, subfolders, files in os.walk(rootDir):
    if folder != rootDir:
        for f in files:
            print(f)

Upvotes: 4

Dan D.
Dan D.

Reputation: 74685

Walk is too much if all you want is the files in the directories in a given directory. For that I would write:

for name in os.listdir(base):
    if os.path.isdir(os.path.join(base, name)):
       for file in os.listdir(os.path.join(base, name)):
           if os.path.isfile(os.path.join(base, name, file)):
               print(os.path.join(base, name, file))

Sure, there is some redundant os.path.join.

Upvotes: 3

Related Questions