Python Pathlib找到了一个文件,但然后放松了吗?

发布于 2025-01-20 05:53:53 字数 1248 浏览 3 评论 0原文

我需要找到一个具有特定命名法的 Excel 文件,但我收到一个奇怪的错误:

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

然后,这会返回一个具有正确命名法的文件的绝对路径(但不是我想要的),然后出现错误:

            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'

我不是当然如何,但在我看来,就像它找到一个带有 glob 的文件,然后当它通过生成器时却无法再次找到它。我使用的是 Python 3.9.10,这发生在 Windows 10 上的 jupyter 笔记本中。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文