0

I have a problem in my project. In my program I have the availability to choose more files if I want. The Problem is, that the return choosen_files has the list of the files but choosen_files which calls the more_files() method is none.

Do you have any suggestion?

Here is my code

import tkinter as tk
from tkinter import filedialog as fd
def get_files(path):

    root = tk.Tk()
    root.withdraw()
    files = fd.askopenfilenames(
        parent=root, title='Choose all files you want', initialdir=path)
    return list(files)


def more_files(choosen_files, path):
    print("choosen files:")
    [print(file) for file in choosen_files]

    wantMoreFiles = input(
            "Do you want to choose more files? [(1) yes, (2) no]").lower()
    if wantMoreFiles in ['1', 'y', 'yes']:
        new_files = get_files(path)
        choosen_files.extend(new_files)
        more_files(choosen_files, path)

    else:
        return choosen_files
               #this has the correct list

files = ['path/file1']
path = 'path'

choosen_files = more_files(files, path)
#this is none

Thank you very much!

1 Answer 1

2

You don't return anything on the last line. Just return more_files(files, path)

Sign up to request clarification or add additional context in comments.

2 Comments

That works for me. Thank you! But can you explain, why a return is needed there? Because when I go into the first if-statement I call the method again. But when I go into the else part, it returns the file list. But why I need than a return in the first condition?
It is the return value that brings in the choosen_files parameter when you do your recursive call. A python function that doesn't explicitly return anything will return None. So then your next call to more_files will have the first parameter being None.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.