Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 537
Var87 Avatar asked Mar 18 '23 15:03

Var87


1 Answers

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))
like image 93
falsetru Avatar answered Apr 08 '23 02:04

falsetru