Jan-09-2018, 08:53 AM
os.walk does exactly that what it should do.
If you need a sorted list of files, then sort with
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
silly example:
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.
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!
All humans together. We don't need politicians!