0

I've noticed that Path().rglob() hangs my script after processing if I pass a large directory (or drive in my case). Is there a way to mitigate that?

from pathlib import Path

for p in Path('C:\\').rglob('Downloads'):
    print(str(p))

The above hangs my script for a few seconds after the search completes and before it prints the results. Anyone have an idea why? Can I eliminate that? If I search a smaller directory, it does not hang.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
malonn
  • 59
  • 1
  • 7

1 Answers1

0

The docs specify the following:

Note: Using the “**” pattern in large directory trees may consume an inordinate amount of time.

I was using rglob() which

is like calling Path.glob() with “**/” added in front of the given relative pattern.

My solution (which helps tremendously) is to not search directories with large amounts of subdirectories. i.e. Don't search the "C:\Windows" directory. Skipping that directory cut the search time by 4.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
malonn
  • 59
  • 1
  • 7