python获取文件夹下所有制定后缀文件名字
时间: 2023-07-27 14:07:18 浏览: 181
可以使用Python的os和glob模块来获取文件夹下所有指定后缀的文件名字,具体代码如下:
```python
import os
import glob
# 文件夹路径
folder_path = '/path/to/folder/'
# 指定后缀
extension = '.txt'
# 使用glob模块来获取所有指定后缀的文件路径
files = glob.glob(os.path.join(folder_path, '*' + extension))
# 遍历文件路径并获取文件名
file_names = [os.path.basename(file) for file in files]
# 输出文件名
print(file_names)
```
在上面的代码中,我们首先指定了文件夹路径和后缀名,然后使用glob模块来获取所有指定后缀的文件路径,最后使用os.path.basename函数来获取文件名并将其存储在file_names列表中。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)