[python] realize the number of files in the output folder
import os
import glob
# 使用glob模块获取文件夹中的所有文件的路径
file_paths = glob.glob("images/*") # image 为文件名; * 表示匹配任意文件名
print(type(file_paths)) # 输出结果为:<class 'list'>;说明file_paths的文件类型为 list
file_count = len(file_paths) # 统计文件数
print("文件总数为:", file_count) # 输出文件总数
[glob] module
glob is a built-in module of Python, which provides a function to search for qualified file paths from file paths according to specific rules , so as to conveniently operate on files.
Specifically, the glob module provides a function called glob() that takes a file path pattern containing wildcards as an argument and returns a list of file paths matching the pattern .
For example, suppose we have a folder named image, which contains some image files in JPG format, we can use the following code to get the file paths of all JPG files:
import glob
jpg_files = glob.glob("image/*.jpg") # 表示匹配‘image’文件夹中所有后缀为‘.jpg’的文件
# ‘jpg_file’则是一个包含符合条件的文件路径的列表
print(jpg_files)
In addition to matching using wildcards, glob also supports using ** to recursively match files in subdirectories.
For example, we can use the following code to get the file paths of all JPG files in the image folder and its subdirectories:
import glob
jpg_files = glob.glob("image/**/*.jpg", recursive=True)
# 表示匹配‘image’文件夹及其子目录中所有后缀为‘.jpg’的文件,recursive=True表示递归搜索子目录。
print(jpg_files)