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

  • SEARCH
  • Home
  • 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 7908257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:51:31+00:00 2026-06-03T11:51:31+00:00

I have a python script that keeps track of several directory names as individual

  • 0

I have a python script that keeps track of several directory names as individual variables. Some of these directories may exist, while others may need to be created at execution time.

For example, if I have this directory tree:

./top
     /a
       /a1
       /a2
     /b
       /b1
     /c

then I currently track all the directory path names with individual variables:

top_path = os.path.join(os.curdir, 'top')
a_path = os.path.join(top_path, 'a')
# etc...

but it would be nice to organise them in some way that respects the directory tree, if not actual OS directory support.

Is there some standard way of managing a directory tree (or any tree of variables) in python? Or should I just store everything in a nested list or tuple, or even make my own class? Something with a nice API like top.a.a1 returning ‘./top/a/a1’ would be great.

  • 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-06-03T11:51:37+00:00Added an answer on June 3, 2026 at 11:51 am

    How about a dict-of-dicts? Something like:

    tree = {
           'top': {
                  'a': {
                       'path': a_pathvar,
                       'a1':
                       'a2':
                   }
                   'b': {
                        'path':  b_pathvar,
                    }
             }
           }
    

    Then you could easily access the path variable for a1 with tree['top']['a']['a1']['path']. You can easily create this tree structure with a single line of code:

    def tree(): return defaultdict(tree)
    

    A nice property of this structure is that intermediate keys need not exist if you try to create a path:

    tree['top']['a']['long']['tree']['path'] = path_var
    

    This will automatically create the intermediate keys for you

    This page goes into more detail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python script that for-loops through some objects and fetches data from
I have a Python script that uses built-in modules but also imports a number
I have a python script that uses threads and makes lots of HTTP requests.
I have a python script that uses plt.show() as it's last instruction. When it
I have a Python script that is running a few ls commands. This script
I have a python script that ends with running a program (iexpress.exe) in a
I have a Python script that reads through a text csv file and creates
I have a python script that retrieves information from a web service and then
I have a Python script that queries a MySQL database every 5 seconds, gathering
I have a Python script that manages a series of CasperJS tasks and processes

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.