python - Pandas Dataframe returns None after recursive function?
问题描述
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)
解决方案
通过将 if 块中的最后一行更改为:
return findSubFoldersMultiple(iter-1,combined_data_container)
否则,您将返回基本情况(else 块)上的值,但不会在非基本情况递归调用链的上游返回它。
推荐阅读
- swift - 初始化中的隐式UnwrappedOptional 与以后
- activemq - 活动 MQ 警告填满日志
- android-studio - 在 Android Studio 上使用 ARM64-v8a 的汇编语言
- c - write() 以错误的顺序打印
- c++ - 显式默认的复制 ctor 生成比手写等效代码更好的代码
- java - Jtextfield 每 3 位自动添加逗号
- c++ - 为什么我需要在链表中返回头部?c++
- html - Safari 溢出-y:滚动与溢出-y:隐藏问题。滚动在 div 下方添加额外的空白区域
- python - 在上下文聊天机器人中添加操作
- sql - 同一组的 SQL 重复数据