2

os.listdir could retrieve a level one dirs and files as ls

In [66]: os.listdir() 
Out[66]: ['build', 'emacs-libvterm']
In [67]: ls           
build/  emacs-libvterm/

How could get a recursive tree dirs as

In [68]: !tree        
.
├── build
└── emacs-libvterm
    ├── CMakeLists.txt
    ├── elisp.c
    ├── elisp.h
    ├── emacs-module.h
    ├── LICENSE
    ├── README.md
    ├── utf8.c
    ├── utf8.h
    ├── vterm.el
    ├── vterm-module.c
    └── vterm-module.h

2 directories, 11 files
AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
  • What code do you have to achieve that so far ? – Arthur Attout Oct 19 '19 at 13:10
  • Possible duplicate of [How do I use os.scandir() to return DirEntry objects recursively on a directory tree?](https://stackoverflow.com/questions/33135038/how-do-i-use-os-scandir-to-return-direntry-objects-recursively-on-a-directory) – Loïc Oct 19 '19 at 14:45

1 Answers1

1

Maybe this will help, if i right understand question. You can use this example. My directory structure:

.
├── empty_folder
├── test
│   ├── sub1
│   │   ├── 1.png
│   │   └── 2.png
│   └── sub2
│       ├── 3.png
│       └── 4.png
└── test.py

code in test.py file:

import os

for path, subdirs, files in os.walk('.'):
    if not subdirs and not files:
        print(path)
    else:
        for name in files:
            print(os.path.join(path, name))

output:

./test.py
./empty_folder
./test/sub2/3.png
./test/sub2/4.png
./test/sub1/2.png
./test/sub1/1.png
Manualmsdos
  • 1,505
  • 3
  • 11
  • 22