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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:09:19+00:00 2026-06-04T07:09:19+00:00

On a mac in python 2.7 when walking through directories using os.walk my script

  • 0

On a mac in python 2.7 when walking through directories using os.walk my script goes through ‘apps’ i.e. appname.app, since those are really just directories of themselves. Well later on in processing I am hitting errors when going through them. I don’t want to go through them anyways so for my purposes it would be best just to ignore those types of ‘directories’.

So this is my current solution:

for root, subdirs, files in os.walk(directory, True):
    for subdir in subdirs:
        if '.' in subdir:
            subdirs.remove(subdir)
    #do more stuff

As you can see, the second for loop will run for every iteration of subdirs, which is unnecessary since the first pass removes everything I want to remove anyways.

There must be a more efficient way to do this. Any ideas?

  • 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-04T07:09:20+00:00Added an answer on June 4, 2026 at 7:09 am

    You can do something like this (assuming you want to ignore directories containing ‘.’):

    subdirs[:] = [d for d in subdirs if '.' not in d]
    

    The slice assignment (rather than just subdirs = ...) is necessary because you need to modify the same list that os.walk is using, not create a new one.

    Note that your original code is incorrect because you modify the list while iterating over it, which is not allowed.

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

Sidebar

Related Questions

I'm writing a Python (2.7) script to build and distribute my Mac (10.7) app.
How can I run a python script in Terminal on Mac without using the
I am a beginner programmer, using python on Mac. I created a function as
I am using Python 3.1.1 on Mac OS X 10.6.2 and need an interface
I'm writing python program to build mac-address cache using pcap. But pcap module for
I'm using Python 3.1.2 (Mac OS X 10.6) and found this weird behavior (I'm
I am trying to build a script in python to mac OS, I need
When I start Python from Mac OS' Terminal.app, python recognises the encoding as UTF-8:
System: Mac OSX 10.6.5, Python 2.6 I try to run the python script below:
I currently have Python 2.6.2 installed on my mac. I am writing a script

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.