Home > front end >  how to get a list of sub directory names, the file names in each and the paths to each of these (Pyt
how to get a list of sub directory names, the file names in each and the paths to each of these (Pyt

Time:01-01

I would like to create a list of sub directories in python. That list would be made of lists. The first item in each sub-list would be the sub directory name and it's path as a tuple. Then the files in that sub directory and their paths.

example: File Structure

assets/
      location1/
                file1.png
                file2.png
                file3.png
      location2/
                file4.png
                file5.png

would return:

[
  [
    ('location1', 'assets/location1'), 
    ('file1.png', 'assets/location1/file1.png'), 
    ('file2.png', 'assets/location1/file2.png'), 
    ('file3.png', 'assets/location1/file3.png')
  ],
  [
    ('location2', 'assets/location2'), 
    ('file4.png', 'assets/location2/file4.png'), 
    ('file5.png', 'assets/location2/file5.png')
  ]
]

Hope that makes sense, thanks in advance for your time!

CodePudding user response:

here is an example I have used in the past:


import os
import re

def crawler(path: str, ignore_hidden: bool = True) -> list[dict]:
    """
    It crawls a directory and returns a list of dictionaries, each dictionary representing a file or
    directory

    Args:
      path (str): The path to the directory you want to crawl.
      ignore_hidden (bool): If True, ignore hidden files and directories. Defaults to True

    Returns:
      A list of dictionaries.
    """

    files = []
    for obj in os.listdir(path):
        obj_path = os.path.normpath(os.path.join(path, obj))
        file = {
            "name": obj,
            "path": os.path.relpath(obj_path),
        }
        pattern = r"^\.\w $"
        match = re.search(pattern, obj, re.IGNORECASE)
        if match and ignore_hidden:
            continue
        if os.path.isfile(obj_path):
            file["type"] = "file"
        else:
            file["type"] = "dir"
            file["files"] = crawler(obj_path)
        files.append(file)
    return files

example output:

In [1]: crawler(os.getcwd())
Out[1]:
[{'name': 'about.txt', 'path': 'about.txt', 'type': 'file'},
 {'name': 'android-chrome-192x192.png',
  'path': 'android-chrome-192x192.png',
  'type': 'file'},
 {'name': 'android-chrome-512x512.png',
  'path': 'android-chrome-512x512.png',
  'type': 'file'},
 {'name': 'apple-touch-icon.png',
  'path': 'apple-touch-icon.png',
  'type': 'file'},
 {'name': 'favicon-16x16.png', 'path': 'favicon-16x16.png', 'type': 'file'},
 {'name': 'favicon-32x32.png', 'path': 'favicon-32x32.png', 'type': 'file'},
 {'name': 'favicon.ico', 'path': 'favicon.ico', 'type': 'file'},
 {'name': 'New folder',
  'path': 'New folder',
  'type': 'dir',
  'files': [{'name': 'New Text Document.txt',
    'path': 'New folder\\New Text Document.txt',
    'type': 'file'}]},
 {'name': 'site.webmanifest', 'path': 'site.webmanifest', 'type': 'file'}]

CodePudding user response:

you can use the python os function you can use os path and os listdir and os path isdir

# is function return all the subdirectories as you ask at the question 
def get_subdirectories(directory):
    subdir = []

    for item in os.listdir(directory):
        path = os.path.join(directory, item)

        if os.path.isdir(path):
            subdir.append([(item, path)])
            subdir  = get_subdirectories(path)
        else:
            subdir[-1].append((item, path))

    return subdir 

  • Related