List only files in a directory?

后端 未结 8 1811
予麋鹿
予麋鹿 2021-02-06 21:49

Is there a way to list the files (not directories) in a directory with Python? I know I could use os.listdir and a loop of os.path.isfile()s, but if th

8条回答
  •  生来不讨喜
    2021-02-06 22:19

    Using pathlib, the shortest way to list only files is:

    [x for x in Path("your_path").iterdir() if x.is_file()]
    

    with depth support if need be.

提交回复
热议问题