Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1069439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:25:20+00:00 2026-05-16T20:25:20+00:00

Follow up of my previous question: Python – How to recursively add a folder's

  • 0

Follow up of my previous question: Python – How to recursively add a folder's content in a dict.

When I build the information dict for each file and folder, I need to merge it to the main tree dict. The only way I have found so far is the write the dict as a text string and have it interpreted into a dict object and then merge it. The issue is that the root object is always the same so it gets overwritten by the new dict and I lose the content.

def recurseItem(Files, Item):
    global Settings
    ROOT = Settings['path']
    dbg(70, "Scanning " + Item)
    indices = path2indice(Item)
    ItemAnalysis = Analyse(Item)
    Treedict = ""#{'" + ROOT + "': "
    i=0
    for indice in indices:
        Treedict = Treedict + "{'" + indice + "': "
        i=i+1
    Treedict = Treedict + repr(ItemAnalysis)
    while i>0:
        Treedict = Treedict + "}"
        i=i-1
    Files = dict(Files.items() + Treedict.items())
return Files

Is there a way to avoid the messy indices construct (i.e. Files[ROOT][fileName][fileName2][fileName3][fileName4] ) which can’t be generated on the fly? I need to be able to update a key’s content without overwriting the root key. Any idea would be much welcomed !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:25:20+00:00Added an answer on May 16, 2026 at 8:25 pm

    Of course you can create nested dictionaries on-the-fly. What about this:

    # Example path, I guess something like this is produced by path2indice?!
    indices = ("home", "username", "Desktop")
    
    tree = {}
    
    d = tree
    for indice in indices[:-1]:
        if indice not in d:
            d[indice] = {}
    
        d = d[indice]
    
    d[indices[-1]] = "some value"
    
    print tree # this will print {'home': {'username': {'Desktop': 'some value'}}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.