listdir()函数

https://blog.csdn.net/lilongsy/article/details/131044866

import os

# 当前目录
dir_path = '/path/to/current/directory'

# 获取当前目录下的所有文件
files = [os.path.join(base_dir, file) for file in os.listdir(base_dir)]

# 遍历文件列表,输出文件名
for file in files:
    print(file)

glob模块中的glob()函数

import os
import glob

# 获取当前目录
directory = os.getcwd()

# 获取所有文件
files = glob.glob(directory + "/*")
files = glob.glob(directory + "/*.html")  #获取定后缀的文件

# 输出所有文件名
for file in files:
    print(file)

walk()函数

import os

dir_path = '当前目录'
for dirpath, dirnames, filenames in os.walk(dir_path):
    for filename in filenames:
        if filename == '需要获取的文件名字':
            print(os.path.join(dirpath, filename))
        if filename.endswith('后缀名'):
            print(os.path.join(dirpath, filename))