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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:38:51+00:00 2026-06-05T17:38:51+00:00

The loop for root, dir, file in os.walk(startdir) works through these steps? for root

  • 0

The loop for root, dir, file in os.walk(startdir) works through these steps?

for root in os.walk(startdir) 
    for dir in root 
        for files in dir
  1. get root of start dir : C:\dir1\dir2\startdir

  2. get folders in C:\dir1\dir2\startdir and return list of folders "dirlist"

  3. get files in the first dirlist item and return the list of files "filelist" as the first item of a list of filelists.

  4. move to the second item in dirlist and return the list of files in this folder "filelist2" as the second item of a list of filelists. etc.

  5. move to the next root in the folder tree and start from 2. etc.

Right? Or does it just get all roots first, then all dirs second, and all files third?

  • 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-05T17:38:53+00:00Added an answer on June 5, 2026 at 5:38 pm

    os.walk returns a generator, that creates a tuple of values (current_path, directories in current_path, files in current_path).

    Every time the generator is called it will follow each directory recursively until no further sub-directories are available from the initial directory that walk was called upon.

    As such,

    os.walk('C:\dir1\dir2\startdir').next()[0] # returns 'C:\dir1\dir2\startdir'
    os.walk('C:\dir1\dir2\startdir').next()[1] # returns all the dirs in 'C:\dir1\dir2\startdir'
    os.walk('C:\dir1\dir2\startdir').next()[2] # returns all the files in 'C:\dir1\dir2\startdir'
    

    So

    import os.path
    ....
    for path, directories, files in os.walk('C:\dir1\dir2\startdir'):
         if file in files:
              print('found %s' % os.path.join(path, file))
    

    or this

    def search_file(directory = None, file = None):
        assert os.path.isdir(directory)
        for cur_path, directories, files in os.walk(directory):
            if file in files:
                return os.path.join(directory, cur_path, file)
        return None
    

    or if you want to look for file you can do this:

    import os
    def search_file(directory = None, file = None):
        assert os.path.isdir(directory)
        current_path, directories, files = os.walk(directory).next()
        if file in files:
            return os.path.join(directory, file)
        elif directories == '':
            return None
        else:
            for new_directory in directories:
                result = search_file(directory = os.path.join(directory, new_directory), file = file)
                if result:
                    return result
            return None
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to loop through all files of .properties extension, in a root folder:C:\ExecutionSDKTest_10.2.2\,
I'm trying to loop through this XML to get specifically to the count fields.
I have a script that will walk a system directory, and get the files
I built a program to loop through words and get their synonyms from www.dicsin.com.br,
I loop through a bunch of directories recursively. Some of them (like D:\$RECYCLE.BIN\S-1-5-20 )
This loop works fine in Main function but when copy this loop code in
Using a for loop, how can I loop through all except the last item
I'm having trouble with an XSLT to loop through all the rows, and then
I am trying to loop through every process in a /proc utility I'm writing
For localization purposes in Wpf I would like to loop through all ResourceManagers in

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.