只取特定类型的后缀名的文件,代码示例
时间: 2024-04-29 10:27:09 浏览: 27
Sure, here's an example code in Python:
```
import os
def filter_files(path, extensions):
filtered_files = []
for file in os.listdir(path):
if file.endswith(tuple(extensions)):
filtered_files.append(file)
return filtered_files
```
To use this code, you just need to call the `filter_files` function with two arguments: the directory path where you want to search for files, and a list of the desired file extensions. For example, if you only want to select files with `.txt` and `.csv` extensions in the `data` directory, you can do:
```
path = 'data/'
extensions = ['.txt', '.csv']
filtered_files = filter_files(path, extensions)
print(filtered_files)
```
This will print a list of all the files in the `data` directory that have either `.txt` or `.csv` extensions.
阅读全文