+3 votes
in Programming Languages by (73.8k 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

+2 votes
by (348k points)
selected by
 
Best answer

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)


...