如何使用 Python 只列出子文件夹(不包含文件)
虽然 Python 的 os.listdir("mydirectory") 会同时列出子文件和子目录,但你可以使用这个简单的列表推导式:
list_subdirs.py
parent_folder = "myfolder"
sub_directories = [
filename for filename in os.listdir(parent_folder)
if os.path.isdir(os.path.join(parent_folder, filename))
]这只会列出 parent_folder 的子目录
Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow