python - Optimized way to get to every file / folder in my dictionary -


i thought had figured out - "yes! finally, succeeded using recursive functions - yeaah.. oh oh nope"

i have dictionary containing data on file structure folders , files:

{     "folder_name": {         type: "dir",         children: {                     "folder_name": {                         type: "dir",                          children: { ... }                     },                     "file_name": {                         type: "file",                         url: '...'                     },                     "file_name": {                         type: "file",                         url: '...'                     }     } } 

i wrote following recursive function element, whenever it's dir, want process contents of dir, whenever there dir in dir, want process dir first, , on. initial call process_dir made dir_name="/root/path", dir_content snippet above , first = true.

def process_dir(dir_name, dir_content, first):     global download_path     if first:         download_path = dir_name + '/'     else:         download_path += clean_path(dir_name) + '/'     full_path = download_path     if not os.path.exists(full_path):         os.makedirs(full_path)     x in dir_content:         type = dir_content[x]['type']         if type == 'dir':             process_dir(x, dir_content[x]['children'], false)         elif type == 'file':             download_file(x, dir_content[x]['size'], dir_content[x]['url']) 

should work expected? it's not working expected me @ least, because it's not going on every node in dictionary sequentially. how can detect after processing contents of dir , returns original call process_dir dir can remove last subdir part full_path?


with of javeed's answer able fix problems resulting in following modified code snippet:

def process_dir(path, new_name, dir_content):     if not dir_content:         return none     new_path = path + '/' + new_name     if not os.path.exists(new_path):         os.makedirs(new_path)     x in dir_content:         type = dir_content[x]['type']         if type == 'dir':             process_dir(new_path, clean_path(x), dir_content[x]['children'])         elif type == 'file':             download_file(new_path + '/' + clean_path(x), dir_content[x]['size'], dir_content[x]['url']) 

some nasty no-nos in one:

global download_path 

please own sanity avoid using global recursion. idea behind recursion stuff operated on bottom or along lines. if reading global, okay cool whatever works fine, if modify outside recursion's call stack, can mess. learn workarounds this, such passing values in (which looks work here).

also, have no exit condition. happens when reach elements have no children or entries? while structure of recursive function can inherently permit exit when done without explicit exit condition, considered bad form exclude it. like

if not dir_content: return none 

or whatnot make easy read.

as whether or not works, depends on download_file , clean_path doing, won't comment on specifics. instead, say, in answer last question, shouldn't need detect when has finished processing contents of subdirectory in order alter global path, there shouldn't global path.

try passing, parameter, path want add on too. insures recursive child call have same base directory parent. no modification needed.

here's few other tips on recursion since mentioned new it:

  1. plan out recursion in abstract way. ask big 3 questions: 1. need in every single frame intended function? 2. how know when done? 3. want back?

  2. use base function recursion, mean, starting function preps information used start recursion. have parameter called first, instead make function gets called once sets base path , starts recursion input. recursive functions should not expected maintain contextual awareness.


Comments