+3 votes
in Programming Languages by (15.0k points)
How can I scan a folder to find all directories and files inside the folder?

I was to use Python for this task.

1 Answer

0 votes
by (26.1k points)

You can use the functions of Python module 'os' to scan a directory to find folders and files in it.

Here is an example to find the list of folders and files inside a folder:

import os

dir_list = []
fl_list = []
for f in os.scandir(os.getcwd()):
    # replace 'os.getcwd()' with path to your folder
    if os.DirEntry.is_dir(f):
        dir_list.append(f.name)
    elif os.DirEntry.is_file(f):
        fl_list.append(f.name)

print("All directories: ", dir_list)
print("All files: ", fl_list)

...