1

I know I can get the immediate contents of a directory with glob.glob("dir/*"). I can also get all of the contents of the subdirectories recusively with something like glob.glob("dir/**/*"), but this will not contain the direct contents of dir/. Is there any glob pattern that will give me the union of the two; everything inside of dir/, recursively?

Edit: In case I am XY probleming too hard here, I am working on a setup.py script and want to include an entire directory as package_data. I am currently doing package_data=["resources/*", "resources/**/*"], but that seems a bit strange to me...

youngmit
  • 800
  • 7
  • 17

1 Answers1

2

You glob pattern dir/**/* is fine to match the files under the dir directory and subdirectories recursively; you need the recursive=True named argument to be passed in:

glob.glob('dir/**/*', recursive=True)

The issue is, without recursive=True, glob will treat the ** (recursive glob) pattern as a usual * (zero or more characters) and will match any files under dir/<subdir>/. When you pass the paramter, ** is treated specially i.e. as a recursive glob pattern.


Also, if your purpose is to just iterate over the files, it's better to use iglob to get an iterator instead of a list:

glob.iglob('dir/**/*', recursive=True)
heemayl
  • 39,294
  • 7
  • 70
  • 76
  • Interesting! The `recursive=True` argument indeed pulls in the contents of `dir/`, but it's behavior seems strange for the `dir/**/*` case specifically. `glob("dir/**", recursive=True)` seems the most natural (and also works). Either way, this is being used in a `setup.py` to specify `package_data`, so I cannot exactly pass the `recursive` argument. – youngmit Oct 31 '19 at 16:01
  • @youngmit What's your eventual plan? What files you want? Under `dir` and some subdirs (what are their names)? – heemayl Oct 31 '19 at 16:03
  • I have a directory called `resources/`. I want its entire contents. Right now I'm doing `package_data=["resources/*", "resources/**/*"]`, but that seems unnatural – youngmit Oct 31 '19 at 16:06
  • 1
    @youngmit Check this: https://stackoverflow.com/a/36693250 Put your recursive `glob` inside `package_files` func. – heemayl Oct 31 '19 at 16:12