0

I've written a simple script to save out the names of various subfolders into a spreadsheet. It seems to be doing its job at every point up to the return statement. It returns None...

If I add a print statement before the return I can see a populated dataFrame.

I guess I'm missing something obvious, would appreciate some help! Thanks

import sys, os, glob
from glob import glob
import pandas as pd

def findSubFoldersMultiple(iter,data_container):
    if iter > 0:
        current_directory = sys.argv[iter]
        directory_reformatted = sys.argv[iter] + "/*/"

        folders = glob(directory_reformatted )
        folders_stripped = [ folder.replace(sys.argv[iter],'').replace('/','') for folder in folders]

        curr_data_container = pd.DataFrame({ current_directory: folders_stripped })
        combined_data_container = pd.concat([data_container,curr_data_container],axis=1)
        findSubFoldersMultiple(iter-1,combined_data_container)
    else:
        print('Populated container in loop: \n' )
        print(data_container)
        return data_container

if len(sys.argv)<2:
    print ("Please specify directory/directories.")
else:
    writer = pd.ExcelWriter('subfolders.xlsx')
    empty_frame = pd.DataFrame({})
    populated_DF = findSubFoldersMultiple(len(sys.argv) - 1, empty_frame)
    print('Returned container: \n' )
    print(populated_DF)
2
  • Hi, could you give an example of what your sys.argv should look like ? Commented Aug 9, 2019 at 10:15
  • At the moment I'm calling it as: 'python subfolders.py /Volumes/SSD1 /Volumes/SSD2/', for example. Will see if I can turn it into a drag and drop situation. Commented Aug 9, 2019 at 10:54

1 Answer 1

2

Catch the return value by changing the last line in the if block to:

return findSubFoldersMultiple(iter-1,combined_data_container)

Otherwise you're returning the value on the base case (the else block), but not returning it further up the chain of non-base case recursive calls.

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

Comments

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.