+3 votes
in Programming Languages by (28.1k 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 (48.7k 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):
    elif os.DirEntry.is_file(f):

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