You cannot specify any of this to os.walk.
However, you can write a function that does what you have in mind.
import os
def list_dir_custom(mindepth=0, maxdepth=float('inf'), starting_dir=None):
""" Lists all files in `starting_dir`
starting from a `mindepth` and ranging to `maxdepth`
If `starting_dir` is `None`,
the current working directory is taken.
"""
def _list_dir_inner(current_dir, current_depth):
if current_depth > maxdepth:
return
dir_list = [os.path.relpath(os.path.join(current_dir, x))
for x in os.listdir(current_dir)]
for item in dir_list:
if os.path.isdir(item):
_list_dir_inner(item, current_depth + 1)
elif current_depth >= mindepth:
result_list.append(item)
if starting_dir is None:
starting_dir = os.getcwd()
result_list = []
_list_dir_inner(starting_dir, 1)
return result_list
EDIT: Added the corrections, reducing unnecessary variable definitions.
2nd Edit: Included 2Rings suggestion to make it list the very same files as find
, i.e. maxdepth
is exclusive.
3rd EDIT: Added other remarks by 2Ring, also changed the path to relpath
to return the output in the same format as find
.