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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:47:18+00:00 2026-05-28T17:47:18+00:00

When I run os.walk() , I get my results in alphanumeric order; starting from

  • 0

When I run os.walk(), I get my results in alphanumeric order; starting from 0, ending at z. Is it possible to reverse this?

So if I have 3 directories; apple/, bananas/, pears/, I would want pears/, bananas/ and apples/ returned.

Obviously I could store all the dirs as a list and then .reverse() but that’s going to take a long time.

  • 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-28T17:47:19+00:00Added an answer on May 28, 2026 at 5:47 pm

    First of all, os.walk() does not specify the order in which the directories are returned, so if I were you I wouldn’t rely on the alphabetic order.

    Having said that, you can choose the order in which the subdirectories are traversed by leaving topdown set to its default value (True), and then sorting dirs in-place:

    import os
    top='/home/aix'
    for root, dirs, files in os.walk(top, topdown=True):
      print root
      dirs.sort(reverse=True)
    

    That’ll make os.walk() traverse the subdirectories in reverse lexicographic order of their names.

    The documentation explains how this works:

    When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again.

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

Sidebar

Related Questions

Possible Duplicate: Get property of object in JavaScript var Terminal = function() { this.walk
I get this error from the first line of the following code: Fatal error:
Possible Duplicate: Tool to convert python indentation from spaces to tabs? I have a
This is probably a case of trying to run before I can walk, however...
I have a service that runs this thread to get GPS coordinates LocationManager lm
When I run through implementing Google Analytics in my app, using this walk through:
I run several queries from mysql command line on my linux box (Fedora Os).
I run a site where it is important to have good and simple URLs
Duplicate: Run .NET exe in linux Hello, Is it possible to make my existing
I run an ASP.NET application on IIS 6.0. Usually I have compression turned on

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.