【问题标题】:How to list all 'files with path' in a windows directory, including all sub-folders in Python?如何列出 Windows 目录中的所有“带路径的文件”,包括 Python 中的所有子文件夹?
【发布时间】:2020-06-20 23:52:36
【问题描述】:

我正在使用带有烧瓶框架的 Python 3.8.3 64 位。

我正在尝试创建一个包含目录中所有文件路径的列表,包括所有子文件夹。

我的代码是

import os
for root, dirs, files in os.walk('/slabs'):
    for f in files: 
        print(os.path.join(root, f))

但是,当我打印出来时,我会得到所有文件及其路径和文件名,其中包括文件名中的目录。

例如,我得到两个条目:

slabs\static\urlimages\1592684282.7557473.png

slabs\static\urlimages1592684282.7557473.png


没有名为 urlimages1592684282.7557473.png 的文件,只有 urlimages\1592684282.7557473.png

如何只获取实际存在的文件或删除同时包含目录和文件名的条目?

提前谢谢你。

【问题讨论】:

    标签: python-3.x windows flask


    【解决方案1】:
    import os
    
    path = os.walk(r'slabs', topdown=True)
    for root, dir, files in path:
        for f in files:
            print(os.path.join(root, f))
            
    

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2016-01-17
      • 1970-01-01
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      • 2018-05-02
      • 1970-01-01
      • 1970-01-01
      • 2023-01-04
      相关资源
      最近更新 更多