📜  Python 仅列出给定目录中的文件 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:26.608000             🧑  作者: Mango

代码示例1
"""Examples from SO (see link)
Obviously the directory can be any directory with 'read access' (not only `os.curdir`)
Doc about the `os` module: https://docs.python.org/3/library/os.html
"""
# /!\  Return a `filter` object, not a `list`
import os
files_ex1 = filter(os.path.isfile, os.listdir(os.curdir))
# List comprehension
files_ex2 = [f for f in os.listdir(os.curdir) if os.path.isfile(f)]