thefence2113
thefence2113

Reputation: 145

Using function in Python to list folder names

I have created a program that creates a function to list the contents of an arbitrary number of folders. The parameter list of the function will be an List of folder names in the program. The return value of the function will be a Dictionary where the key is the folder, and the value is a list of the contents of that folder. I have created the code yet keep getting errors.

I have already tried using a function I previously coded and worked fine, but I cannot get it to work for this particular program.

import os
folder_path = "C:\\Users\\Anonymous\\Desktop\\Test"
# function header
# def function_name(parameter_list)
def expand_folders(folder_names):
# declare an empty dictionary
    result_dict= {}
# for each folder name
for name in folder_names:
# get the full path of folder
    folder = os.path.join(folder_path, name)
# store name as key and the list of files as value
# after this line the dictionary will have one key-value pair
    result_dict[name] = os.listdir(folder)
return result_dict



print("Folders List")
print(os.listdir(folder_path))
print()
# an empty list to keep selected folder names
folder_names = []
while True:
# get folder name
    name = input("Select a folder to expand: ")
    if(name == 'Q' or name == 'q'):
        break
folder_names.append(name)
result_dict = expand_folders(folder_names)
print(result_dict)

The expected results would have the program ask to select a folder, such as Folders List: [1,2,3,4,5]

Select a folder to list: 1 Select a folder to list: 3 Select a folder to list: Q {'1': ['file11.txt'], '3': ['file13.txt']}

Upvotes: 1

Views: 243

Answers (2)

PineconeDude
PineconeDude

Reputation: 98

A couple things are wrong:

  1. Your indentation is inconsistent. As @Smart Pointer mentioned, indentation is very important in Python, as it is the only way the interpreter knows what code is inside a specific loop/function.

  2. folder = os.path.join(folder_path, name) will result in the folder variable being set to whatever folder_path is, plus the last folder name in folder_names.

Here is the revised code:

import os
folder_path = "C:\\"
# function header
# def function_name(parameter_list)
def expand_folders(folder_names):
    # declare an empty dictionary
    result_dict= {}
    # for each folder name
    folder = folder_path
    for name in folder_names:
        # get the full path of folder
        folder = os.path.join(folder, name)
    # store name as key and the list of files as value
    # after this line the dictionary will have one key-value pair
    result_dict[name] = os.listdir(folder)
    return result_dict



print("Folders List")
print(os.listdir(folder_path))
print()
# an empty list to keep selected folder names
folder_names = []
while True:
    # get folder name
    name = input("Select a folder to expand: ")
    if(name == 'Q' or name == 'q'):
        break
    folder_names.append(name)
    result_dict = expand_folders(folder_names)
    print(result_dict)

Upvotes: 3

truth
truth

Reputation: 1186

Your code is fine, just fix the indentation (it is really important in Python):

def expand_folders(folder_names):
    result_dict= {}
    for name in folder_names:
        folder = os.path.join(folder_path, name)
        result_dict[name] = os.listdir(folder)
    return result_dict

Upvotes: 0

Related Questions