有没有一种方法可以扫描目录以查看有多少种特定类型的文件? [英] Is there a way to scan a directory to see how many files of certain type there is?

查看:74
本文介绍了有没有一种方法可以扫描目录以查看有多少种特定类型的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,如果我有一个类似以下的目录,则可以通过它的名称扫描带有特定代码的文件数量.例如,如果我想从17042020开始的文件数在下面的目录中是6?

So if I have a directory like below is there a way to scan how many files with a specific code in its name. For example if I want the number of files that start with 17042020 which would be 6 from the directory below?

1704202001-01.csv
1704202001-02.csv
1704202002-01.csv
1704202002-02.csv
1704202003-01.csv
1704202003-02.csv
001.png
002.pdf
003.docx
004.jpg

推荐答案

,您可以使用 pathlib 模块或简单的 glob :

you can use the pathlib module or just plain glob :

 from pathlib import Path
 folder = Path(dir_of_files)
 specific_files = list(folder.glob('1704202001-*.csv')
 #or : list(folder.rglob('1704202001-*.csv') ... this will recursively search thru every subfolder for files that match this

 print(specific_files)

这篇关于有没有一种方法可以扫描目录以查看有多少种特定类型的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆