【问题标题】:How to create a python list with the number of file in each sub directory of a directory如何使用目录的每个子目录中的文件数创建一个python列表
【发布时间】:2020-08-28 19:30:26
【问题描述】:

我有一个包含 6 个子目录的主目录(根目录)。 我想计算每个子目录中存在的文件数并将所有文件添加到一个简单的 python 列表中

对于这个结果:mylist = [497643, 5976, 3698, 12, 456, 745]

我在该代码上被阻止:

import os, sys
list = []
# Open a file
path = "c://root"
dirs = os.listdir( path )

# This would print all the files and directories
for file in dirs:
   print (file)

#fill a list with each sub directory number of elements
for sub_dir in dirs:
    list = dirs.append(len(sub_dir))

我尝试填写列表没有用,我正处于最佳状态...

找到一种方法来迭代主目录的子目录并使用应用于每个子目录的函数填充列表将大大提高我实际数据科学项目的速度!

感谢您的帮助

亚伯

【问题讨论】:

标签: python list listdir


【解决方案1】:

您可以使用os.path.isfileos.path.isdir

res = [len(list(map(os.path.isfile, os.listdir(os.path.join(path, name))))) for name in os.listdir(path) if os.path.isdir(os.path.join(path, name))]
print(res)

使用 for 循环

res = []
for name in os.listdir(path):
    dir_path = os.path.join(path, name)
    if os.path.isdir(dir_path):
        res.append(len(list(map(os.path.isfile, os.listdir(dir_path)))))

【讨论】:

  • 使用这两种解决方案,我有错误消息: 7 list = [] ----> 8 res = [len(list(map(os.path.isfile, os.listdir(os .path.join(path, name))))) for name in os.listdir(path) if os.path.isdir(os.path.join(path, name))] 9 print(res) TypeError: 'list ' 对象不可调用
  • 您在代码中的某处使用了list 作为变量名,这就是您收到错误的原因。不要使用这个list = [] 将名称更改为其他可以使用的名称
【解决方案2】:

您需要在每个子目录上使用 os.listdir。当前代码只取文件路径的长度。

import os, sys
list = []
# Open a file
path = "c://root"
dirs = os.listdir( path )

# This would print all the files and directories
for file in dirs:
   print (file)

#fill a list with each sub directory number of elements
for sub_dir in dirs:
    temp = os.listdir(sub_dir)
    list = dirs.append(len(temp))

在代码中加入这一行会列出子目录

【讨论】:

  • 如果子目录中有子目录,则需要使用@sushanth 链接的解决方案
  • ```FileNotFoundError Traceback (last recent call last) in 11 #fill a list with each sub directory number of elements 12 for sub_dir in dirs: ---> 13 temp = os.listdir(sub_dir) 14 list = dirs.append(len(temp)) FileNotFoundError: [WinError 3] Le chemin d'accès spécifié est introuvable: 'AMARYLLIDACEAE'
  • 我在添加这行代码时遇到了这个错误。考虑到“AMARYLLIDACEAE”是第一个子目录。我在 Jupyter 笔记本上工作
【解决方案3】:

你快到了:

import os, sys

list = []

# Open a file
path = "c://root"
dirs = os.listdir(path)

# This would print all the files and directories
for file in dirs:
    print(file)

for sub_dir in dirs:
    if os.path.isdir(sub_dir):
        list.append(len(os.listdir(os.path.join(path, sub_dir))))

print(list)

【讨论】:

    【解决方案4】:

    作为替代方案,您也可以使用glob 模块来完成此任务和其他相关任务。 我创建了一个 test 目录,其中包含 3 个子目录 lmk,每个子目录包含 3 个测试文件。

    import os, glob
      
    list = []
    path = "test" # you can leave this "." if you want files in the current directory
    
    for root, dirs, files in os.walk(path, topdown=True):
       for name in dirs:
         list.append(len(glob.glob(root + '/' +  name + '/*')))
    
    print(list)
    

    输出:

    [3, 3, 3]
    

    【讨论】:

    • 你的解决方案对我的问题来说是更好的:) 它在一个列表中创建两个列表:第一个包含完整路径 + 文件名,第二个包含每个目录中的文件数.我只需要第二个列表,我知道如何删除第一个列表。但是如何直接创建一个简单的列表,只包含每个子目录中的文件数? (无路径和目录)
    • @Abel 我已经更新了代码。您需要path,因为您需要从某个地方开始。对于当前目录,您可以将其保留为 "."。然后,os.walk 将为您完成这项工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多