根据您在功能中提供的扩展名和文件夹获取文件数量

发布于 2025-02-13 04:45:11 字数 679 浏览 0 评论 0原文

我想设计一个函数如下:

 explore("jpg", "../test_media")

outputSample:

{'../test_media':1,'../test_media/travel_photos':3}

也就直接在该文件夹中。如果文件夹不包含我们想要的文件,则不应在字典中。

我自己写的代码:

import os


def explore(ttype, address):
    
    list_dir = list(os.walk(address))
    directoirs = dict()
    for x in list_dir:
        count = 0
        if x[2]:
            for y in x[2]:
                t = y.split('.')
                if t[1].lower() == ttype:
                    count += 1
        if count:
            directoirs[str(x[0])] = count

    return directoirs

但是它没有给出所需的输出

I want to design a function as follows:

 explore("jpg", "../test_media")

outputsample:

{'../test_media':1,'../test_media/travel_photos':3}

That is, the output of the function should be a dictionary where each key is the address of a folder and each value is the number of files with that extension directly in that folder. If a folder does not contain the file we want, it should not be in the dictionary.

The code I wrote myself:

import os


def explore(ttype, address):
    
    list_dir = list(os.walk(address))
    directoirs = dict()
    for x in list_dir:
        count = 0
        if x[2]:
            for y in x[2]:
                t = y.split('.')
                if t[1].lower() == ttype:
                    count += 1
        if count:
            directoirs[str(x[0])] = count

    return directoirs

But it does not give the desired output

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终陌 2025-02-20 04:45:11

您可以这样编辑功能:
导入OS

def explore(extension, addr):
    result = dict()
    for obj in os.walk(addr):
        for name in obj[2]:
            if "." in name and name.split('.')[-1].lower() == extension.lower():
                try:
                    result[obj[0]] += 1
                except KeyError:
                    result[obj[0]] = 1
    return result

样本输出:

   >>> explore("jpg", "../test_media")
{'../test_media':1,'../test_media/travel_photos':3}
>>> explore("txt", "../test_media/travel_photos")
{'../test_media/travel_photos':1}
>>> explore("mkv", "../test_media/")
{}

 Directory
     ├── source
        │     └──Explorer.py
        │
        test_media
        ├── IMG_2235.jpg
        ├── travel_photos
        │   ├── 2018-11-09_11-27-14.3gP
        │   ├── IMG_20171017_052418.jpg
        │   ├── 20180311_214539.JPG
        │   ├── IMG_2237.jpg   
        │   └── note.tXt
        └── vid1
            ├── images
            │   └── JPG
            └── VID_20170425_184731.mp4
  

you can edit function like this :
import os

def explore(extension, addr):
    result = dict()
    for obj in os.walk(addr):
        for name in obj[2]:
            if "." in name and name.split('.')[-1].lower() == extension.lower():
                try:
                    result[obj[0]] += 1
                except KeyError:
                    result[obj[0]] = 1
    return result

sample output:

   >>> explore("jpg", "../test_media")
{'../test_media':1,'../test_media/travel_photos':3}
>>> explore("txt", "../test_media/travel_photos")
{'../test_media/travel_photos':1}
>>> explore("mkv", "../test_media/")
{}

 Directory
     ├── source
        │     └──Explorer.py
        │
        test_media
        ├── IMG_2235.jpg
        ├── travel_photos
        │   ├── 2018-11-09_11-27-14.3gP
        │   ├── IMG_20171017_052418.jpg
        │   ├── 20180311_214539.JPG
        │   ├── IMG_2237.jpg   
        │   └── note.tXt
        └── vid1
            ├── images
            │   └── JPG
            └── VID_20170425_184731.mp4
  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文