python print 列出所有
时间: 2023-11-23 20:06:34 浏览: 81
要使用Python的print函数列出特定文件夹下的所有文件名,你可以使用以下代码:
```
import os
folder_path = 'E:\机器学习\Python数学建模算法与应用课件'
file_names = [filename for filename in os.listdir(folder_path) if filename.endswith(('.pptx','.txt'))]
for name in file_names:
print(name)
```
这段代码首先导入了os模块,然后使用listdir函数获取指定文件夹下的所有文件名,并使用列表推导式过滤出只以'.pptx'和'.txt'结尾的文件名。最后使用print函数逐行输出文件名。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [python列出文件夹下所有文件](https://blog.csdn.net/qq_45960449/article/details/123452095)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [Python笔记.docx](https://download.csdn.net/download/bxy0806/88235414)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [Python 列出目录中的文件](https://blog.csdn.net/allway2/article/details/124176562)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文