4

A directory tree looks like this:

DirA--
     |
     -- Map
     |
     -- Fig--
            |
            --file.png
     |
     -- Data--
              |
              -- file.xls
              |
              -- file.csv

There are multiple directories, containing multiple files. I would like to get full path to those files that are found in Data directory only.

This is what I have so far:

dirlist = []
thisdir = os.getcwd()

for root, dirs, files in os.walk(thisdir):
    for d in dirs:
        if d.startswith("Data"):
            dirlist.append(os.path.join(root, d))
Acorn
  • 24,970
  • 5
  • 40
  • 69
olyashevska
  • 427
  • 4
  • 18

2 Answers2

3

To get only Data directory files, you will need to combine root and files.

for root, dirs, files in os.walk(thisdir):
        if "Data" in root: # try using in instead of startswith
           for f in files:
            dirlist.append(os.path.join(root, f))

Trying to do it using 'dirs' In case of 'dirs', you don't have access to the files. For example, when root is DirA, you will have --Data-- in your dirs list but you will have no access to the files of --Data-- folder.

Faizan
  • 268
  • 2
  • 9
0
import os
from os import listdir
from os.path import isfile, join

rootdir = os.getcwd()

folder_name = "Data"

def get_files(path):
    onlyfiles = [f for f in listdir(path) if isfile(join(path, f))]
    return onlyfiles

def get_search_files(start_path, folder_name):
    for subdir, dirs, files in start_path:
        for x in dirs:
            if x == folder_name:
                data_folder_path = os.path.join(subdir, x)
                dirlist = get_files(data_folder_path)
                return dirlist

dirlist = get_search_files(os.walk(rootdir), folder_name)

jacob galam
  • 781
  • 9
  • 21