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 6938171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:27:31+00:00 2026-05-27T12:27:31+00:00

I do atomistic modelling, and use Python to analyze simulation results. To simplify work

  • 0

I do atomistic modelling, and use Python to analyze simulation results. To simplify work with a whole bunch of Python scripts used for different tasks, I decided to write simple GUI to run scripts from it.

I have a (rather complex) directory structure beginning from some root (say ~/calc), and I want to populate wx.TreeCtrl control with directories containing calculation results preserving their structure. The folder contains the results if it contains a file with .EXT extension. What i try to do is walk through dirs from root and in each dir check whether it contains .EXT file. When such dir is reached, add it and its ancestors to the tree:

def buildTree(self, rootdir):
    root = rootdir
    r = len(rootdir.split('/'))
    ids = {root : self.CalcTree.AddRoot(root)}
    for (dirpath, dirnames, filenames) in os.walk(root):
        for dirname in dirnames:
            fullpath = os.path.join(dirpath, dirname)
            if sum([s.find('.EXT') for s in filenames]) > -1 * len(filenames):
                ancdirs = fullpath.split('/')[r:]
                ad = rootdir
                for ancdir in ancdirs:
                    d = os.path.join(ad, ancdir)
                    ids[d] = self.CalcTree.AppendItem(ids[ad], ancdir)
                    ad = d

But this code ends up with many second-level nodes with the same name, and that’s definitely not what I want. So I somehow need to see if the node is already added to the tree, and in positive case add new node to the existing one, but I do not understand how this could be done. Could you please give me a hint?

Besides, the code contains 2 dirty hacks I’d like to get rid of:

  1. I get the list of ancestor dirs with splitting the full path in \
    positions, and this is Linux-specific;

  2. I find if .EXT file is in the directory by trying to find the extension in the strings from filenames list, taking in account that s.find returns -1 if the substring is not found.

Is there a way to make these chunks of code more readable?

  • 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-27T12:27:31+00:00Added an answer on May 27, 2026 at 12:27 pm

    First of all the hacks:

    1. To get the path seperator for whatever os your using you can use os.sep.

    2. Use str.endswith() and use the fact that in Python the empty list [] evaluates to False:

      if [ file for file in filenames if file.endswith('.EXT') ]:

    In terms of getting them all nicely nested you’re best off doing it recursively. So the pseudocode would look something like the following. Please note this is just provided to give you an idea of how to do it, don’t expect it to work as it is!

    def buildTree(self, rootdir):
        rootId = self.CalcTree.AddRoot(root)
    
        self.buildTreeRecursion(rootdir, rootId)
    
    def buildTreeRecursion(self, dir, parentId)
        # Iterate over the files in dir
        for file in dirFiles: 
            id = self.CalcTree.AppendItem(parentId, file) 
    
            if file is a directory:
                self.buildTreeRecursion(file, id)
    

    Hope this helps!

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

Sidebar

Related Questions

I was just looking at this stackoverflow question: Display web browser settings And that

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.