使用 Python 获取指定路径下指定格式的文件,你可以使用 'os.listdir()' 函数来获取指定路径下的所有文件和文件夹,然后使用文件名的后缀来过滤出指定格式的文件。

下面是一个例子,假设你要获取 '/path/to/directory' 路径下的所有 '.txt' 文件:

import os

path = '/path/to/directory'
extension = '.txt'

files = os.listdir(path)
txt_files = [file for file in files if file.endswith(extension)]

print(txt_files)

这将输出 '/path/to/directory' 路径下所有的 '.txt' 文件的文件名列表。

Python 获取指定路径下指定格式的文件 - 代码示例

原文地址: https://www.cveoy.top/t/topic/ph5W 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录