Skip to main content
Provided an example directory structure and made the example code snippet actually work.
Source Link
Johan Dahlin
  • 26.7k
  • 6
  • 43
  • 55

How to use glob() to find files recursively?

This is whatI would like to list all files recursively in a directory. I currently have a directory structure like this:

  • src/main.c
  • src/dir/file1.c
  • src/another-dir/file2.c
  • src/another-dir/nested/files/file3.c

I've tried to do the following:

from glob import glob glob(os.path.join('src','*.c')) 

But this will only get be files directly in the src subfolder, e.g. I get main.c but I want to search the subfolders of srcwill not get file1.c, file2.c etc. Something like this would work:

from glob import glob glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*','*','*.c')) 

But this is obviously limited and clunky., how can I do this properly?

How to use glob() to find files recursively?

This is what I have:

glob(os.path.join('src','*.c')) 

but I want to search the subfolders of src. Something like this would work:

glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*','*','*.c')) 

But this is obviously limited and clunky.

How to use to find files recursively?

I would like to list all files recursively in a directory. I currently have a directory structure like this:

  • src/main.c
  • src/dir/file1.c
  • src/another-dir/file2.c
  • src/another-dir/nested/files/file3.c

I've tried to do the following:

from glob import glob glob(os.path.join('src','*.c')) 

But this will only get be files directly in the src subfolder, e.g. I get main.c but I will not get file1.c, file2.c etc.

from glob import glob glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*','*','*.c')) 

But this is obviously limited and clunky, how can I do this properly?

Question Protected by Johan Dahlin
Question Unprotected by Johan Dahlin
Added [fnmatch] and [filesystems] tags
Link
fish2000
  • 4.4k
  • 2
  • 41
  • 80
Edited title to better express question being asked and removed language tag.
Link
martineau
  • 124.1k
  • 29
  • 181
  • 319

Use a Glob How to use glob() to find files recursively in Python?

Question Protected by cs95
the `glob` command was capitalized, as Python is case sensitive this will not work
Source Link
Loading
Source Link
Ben Gartner
  • 15k
  • 10
  • 39
  • 37
Loading