What is best alternative to find in python (3) for recursively returning all files and folders in a directory structure?
I want something similar to:
find ~/x/y/ > ~/matches.txt
I rewrote a tip from another question and got something that kind of works, but it has some problems:
matches = glob.glob("/Users/x/y/*/*)
This will not work if there are any files in "~/x/y/" which could happen I'm also not sure it's a robust or idiomatic way to do this.
So what's the best way to copy the above find command in Python?
You can use os.walk
:
To get directory, file name list:
import os
matches = []
for dirpath, dirnames, filenames in os.walk(os.path.expanduser('~/x/y')):
matches.extend(os.path.join(dirpath, x) for x in dirnames + filenames)
To write the file list to text file:
import os
with open(os.path.expanduser('~/matches.txt'), 'w') as f:
for dirpath, dirnames, filenames in os.walk(os.path.expanduser('~/x/y')):
for x in dirnames + filenames:
f.write('{}\n'.format(os.path.join(dirpath, x)))
os.path.expanduser
is used to replace ~
with home directory path.
Alternative using pathlib.Path.rglob
which available since Python 3.4:
import os
import pathlib
matches = list(map(str, pathlib.Path(os.path.expanduser('~/x/y')).rglob('*')))
import os
import pathlib
with open(os.path.expanduser('~/matches.txt'), 'w') as f:
for path in pathlib.Path(os.path.expanduser('~/x/y')).rglob('*'):
f.write('{}\n'.format(path))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With