name for name in os.listdir(dir_path)
时间: 2024-03-06 10:18:40 浏览: 163
Python对文件和目录进行操作的方法(file对象/os/os.path/shutil 模块)
要从给定的目录路径中获取所有文件和文件夹的名称,您可以使用以下代码片段:
import os
dir_path = "your_directory_path"
names = os.listdir(dir_path)
for name in names:
print(name)
这将打印出目录中的所有文件和文件夹的名称。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [os.path.join()函数,os.walk()方法,os.listdir()方法,import shutil模块](https://blog.csdn.net/wyyyyyyfff/article/details/104381429)[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_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文