【问题标题】:list of name and path of files in a directory and subdirectory where one or more subdir have a point in the middle python目录和子目录中文件的名称和路径列表,其中一个或多个子目录在 python 中间有一个点
【发布时间】:2022-11-03 22:00:55
【问题描述】:

我正在尝试从目录中获取路径和文件名,包括子目录中的那些。问题是某些子文件夹的名称中有一个或多个点。
所以当我执行这段代码时

listaFile=glob.glob('c:\test\ID_1'+/**/*.*',recursive= True)

我明白了
c:\test\ID_1\file.e1.txt
c:\test\ID_1\file.e2.doc
c:\test\ID_1\subfolder1\file1.txt
c:\test\ID_1\sub.folder2(而不是 c:\test\ID_1\sub.folder2\file1.txt)
谢谢大家!

【问题讨论】:

    标签: python glob


    【解决方案1】:

    您需要将其过滤掉,检查它是文件还是文件夹。一种简单的方法是直接使用 pathlib 而不是 glob。下面的例子。

    listaFile = [path for path in pathlib.Path(r"c:	estID_1").rglob("*.*") if path.is_file()]
    

    【讨论】:

      猜你喜欢
      • 2012-09-02
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多