0

I need to find an excel file with certain nomenclature but I'm receiving an odd error:

ppp = pathlib.Path('A:\\bsolute\path_to\dir_being_searched').glob('**/*edit.xlsx') 
for iii in ppp:
    print(iii.absolute())

This then returns one absolute path to a file (but not the one I want) with the right nomenclature and then errors out with:

            FileNotFoundError                         Traceback (most recent call last)
    Input In [19], in <cell line: 2>()
          1 ppp = pathlib.Path('A:\\bsolute\path_to\dir_being_searched').glob('**/*edit.xlsx')
    ----> 2 for iii in ppp:
          3     print(iii.absolute())
    
    etc.

    File C:\Anaconda3.7\envs\env_name\lib\pathlib.py:558, in _WildcardSelector._select_from(self, parent_path, is_dir, exists, scandir)
            556 def _select_from(self, parent_path, is_dir, exists, scandir):
            557     try:
        --> 558         with scandir(parent_path) as scandir_it:
            559             entries = list(scandir_it)
            560         for entry in entries:
        
        FileNotFoundError: [WinError 3] The system cannot find the path specified: 'A:\\bsolute\\path_to\\dir_being_searched\\more\\dirs'

I'm not sure how, but it looks to me like its finding a file with the glob and then failing to find it again when it goes through the generator. I'm using Python 3.9.10 and this is happening in a jupyter notebook on Windows 10.

Messypuddle
  • 412
  • 6
  • 12
  • 1
    What do you see if place `print(list(ppp))` before your `for` loop and change your `for` loop to `for iii in pathlib.Path('A:\\bsolute\path_to\dir_being_searched').glob('**/*edit.xlsx'):`? – Wayne Apr 08 '22 at 16:42
  • The print statement before the loop produces the same error. Replacing `ppp` with `pathlib.Path('A:\\bsolute\path_to\dir_being_searched').glob('**/*edit.xlsx'):` prints 2 absolute paths for files with the correct nomenclature and then produces the same error. – Messypuddle Apr 11 '22 at 22:21
  • 1
    Okay, with the code that produces the correct nomenclature you should be able to use that in a try / except so you can collect the ones with the correct nomeclature and not error out with the file not found error? Alternatively (**maybe easier**?), put another `if` statement before your `print(iii.absolute())` line and only print the absolute path if it is not a directory? Try `if not os.path.isdir(iii)` or `if os.path.isfile(iii)`. You'll need to add `import os.path` towards start of your code. – Wayne Apr 12 '22 at 16:17
  • Well, I tried a try : print / except :continue structure, as well as the if statements to check isfile and isdir. It still spits out two files with the correct nomenclature and then gives the same error. To confirm its a problem with the pathlib.Path.glob I included a ppp=list(ppp) before the loop and the debugger pointed to that line. It looks like the glob created a bad entry in the itterator – Messypuddle Apr 25 '22 at 20:51
  • 1
    I was trying to look in the [issues with glob](https://github.com/search?o=desc&q=org%3Apython+glob&s=updated&type=Issues) and see if something related has already been reported. I sorted by most recent. [This one](https://github.com/python/cpython/issues/90208) or [this one](https://github.com/python/cpython/issues/89769#issuecomment-1093935104) looked vaguely similar to me; however, you may see a better match? Often in those threads someone proposes a useful workaround. At the very least you can get a sense of whether it is being addressed. – Wayne Apr 25 '22 at 21:16
  • Maybe [this one](https://github.com/python/cpython/issues/85732#issue-1199042325) is related? – Wayne Apr 25 '22 at 21:18

0 Answers0