Equivalent To The Find Coreutil Command In Python 3 For Recursively Returning All Files And Folders In A Directory Structure?
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
Solution 1:
You can use os.walk
:
To get directory, file name list:
import os
matches = []
for dirpath, dirnames, filenames inos.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 inos.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:
forpathin pathlib.Path(os.path.expanduser('~/x/y')).rglob('*'):
f.write('{}\n'.format(path))
Solution 2:
Kinda like os.dirwalk, but with new pathlib constructs:
import pathlib
def pathlibs_dirwalk(dir_path):
dir_path =Path(dir_path)
return { f.relative_to(dir_path) for f in dir_path.rglob('*') }
This one also chops off the root, so it's like like GNU find's version:
find /usr/local/gdb-8.2a -printf '%P\n'
lib/libopcodes.a
lib/libinproctrace.so
bin
bin/gdb
bin/gcore
Solution 3:
As much as I like globs, since your question specifically says python 3, I'm more than happy to recommend using os.scandir
+ yield from
to simplify the approach.
import os
def find(path):
yieldpathifos.path.isdir(path):
for i inos.scandir(path):
yield from find(i.path)
for i infind('.'):
print(i)
Post a Comment for "Equivalent To The Find Coreutil Command In Python 3 For Recursively Returning All Files And Folders In A Directory Structure?"