python筛选文件名称
时间: 2023-10-19 21:07:01 浏览: 98
使用Python筛选文件名称的方法有多种。根据提供的代码段,可以使用以下方法进行文件筛选:
方法1:
```python
import os
file_path = r'./data'
target_name = 'name.xls'
filelist = os.listdir(file_path)
filtered_files = [file for file in filelist if target_name in file]
print(filtered_files)
```
方法2:
```python
import os
file_path = r'path1'
target_name = '2'
filtered_files = []
for file in os.listdir(file_path):
if target_name in file:
filtered_files.append(file)
print(filtered_files)
```
方法3:
```python
import os
file_path = r'path1'
target_names = ['2', '3']
filtered_files = []
for file in os.listdir(file_path):
for target_name in target_names:
if target_name in file:
filtered_files.append(file)
print(filtered_files)
```
阅读全文
相关推荐
















