根据您在功能中提供的扩展名和文件夹获取文件数量
我想设计一个函数如下:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以这样编辑功能:
导入OS
样本输出:
you can edit function like this :
import os
sample output: