Disclaimer os.walk
is just fine, I'm here to provide a easier solution.
If you're using python 3.5 or above, you can use glob.glob
with '**'
and recursive=True
For example: glob.glob(r'C:\**', recursive=True)
Please note that getting the entire file list of C:\ drive can take a lot of time.
If you don't need the entire list at the same time, glob.iglob
is a reasonable choice. The usage is the same, except that you get an iterator instead of a list.
To print everything under C:\
for filename in glob.iglob(r'C:\**', recursive=True):
print(filename)
It gives you output as soon as possible.
However if you don't have python 3.5 available, you can see the source of glob.py and adapt it for your use case.