1

I should do it wiht only import os

I have problem, that i don't know how to make program after checking the specific folder for folders to do the same for folders in these folders and so on.

Oby1Kenoby
  • 11
  • 1
  • 1
    Look at this answer [https://stackoverflow.com/a/973488/12613186](https://stackoverflow.com/a/973488/12613186) – weAreStarsDust Jan 30 '20 at 17:28
  • What specifically is your problem? If you are trying to get your script to print out the file structure of the directory it is in, this may help: https://www.tutorialspoint.com/python/os_walk.htm – OakenDuck Jan 30 '20 at 17:31

2 Answers2

2

The quickest way is using os.walk like this:

import os
path = '.'
folder_paths = [
    paths[0] for paths in os.walk(path)
]

print(folder_paths)

If you need a custom recursive function you can use os.listdir and os.path.isdir instead:

def print_sub_dirs(path):
    folder_paths = [
        os.path.join(path, folder) for folder in os.listdir(path) if os.path.isdir(os.path.join(path, folder))
    ]
    for folder_path in folder_paths:
        print(folder_path)
        print_sub_dirs(folder_path)

print_sub_dirs('.')
marcos
  • 4,473
  • 1
  • 10
  • 24
0

You can use os.walk(directory)

mirxzh
  • 1