Python Forum
why i don't like os.walk()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why i don't like os.walk()
#6
os.walk does exactly that what it should do.

If you need a sorted list of files, then sort with sorted.
If you need to exclude files from a list, then write your function for it.
If you want to check for symlinks, do it in the loop with os.path.islink.

silly example:
def exclude_filter(file):
    return True if file.startswith('.') else False

for root, dirs, files in os.walk('.'):
    for file in sorted(files):
        file_path = os.path.join(root, file)
        if os.path.islink(file_path):
            continue
        if exclude_filter(file):
            continue
        print(file_path)
If you need a special kind of os.wlak, which is doing everything, sorry. You have to write your own.
What you want to have, can be in somewhere in special library for this special task.
For example, if you want to sort the traverse by directories, it's getting complicated with this function.

This kind of super special function will never get into the standard library. If you ask 10 people, you will get 40 requests for this implementation.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
why i don't like os.walk() - by Skaperen - Jan-09-2018, 05:50 AM
RE: why i don't like os.walk() - by Larz60+ - Jan-09-2018, 06:35 AM
RE: why i don't like os.walk() - by Gribouillis - Jan-09-2018, 07:44 AM
RE: why i don't like os.walk() - by Skaperen - Jan-09-2018, 08:39 AM
RE: why i don't like os.walk() - by Gribouillis - Jan-09-2018, 08:48 AM
RE: why i don't like os.walk() - by DeaD_EyE - Jan-09-2018, 08:53 AM
RE: why i don't like os.walk() - by wavic - Jan-09-2018, 05:09 PM
RE: why i don't like os.walk() - by Gribouillis - Jan-09-2018, 05:18 PM
RE: why i don't like os.walk() - by Skaperen - Jan-10-2018, 03:26 AM
RE: why i don't like os.walk() - by Skaperen - Jan-10-2018, 04:44 AM
RE: why i don't like os.walk() - by Larz60+ - Jan-10-2018, 05:03 AM
RE: why i don't like os.walk() - by Gribouillis - Jan-10-2018, 05:12 AM
RE: why i don't like os.walk() - by Skaperen - Jan-10-2018, 05:54 AM
RE: why i don't like os.walk() - by Skaperen - Jan-10-2018, 07:01 AM
RE: why i don't like os.walk() - by Gribouillis - Jan-10-2018, 10:45 PM
RE: why i don't like os.walk() - by snippsat - Jan-10-2018, 11:59 PM
RE: why i don't like os.walk() - by Skaperen - Jan-11-2018, 01:37 AM
RE: why i don't like os.walk() - by Larz60+ - Jan-11-2018, 02:23 AM
RE: why i don't like os.walk() - by Skaperen - Jan-11-2018, 06:00 AM
RE: why i don't like os.walk() - by Gribouillis - Jan-11-2018, 07:07 AM
RE: why i don't like os.walk() - by Skaperen - Jan-11-2018, 08:39 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  examples using os.walk() Skaperen 12 7,611 Mar-22-2021, 05:56 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020