List only files in a directory?

后端 未结 8 1814
予麋鹿
予麋鹿 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:00

    Since Python 3.6 you can use glob with a recursive option "**". Note that glob will give you all files and directories, so you can keep only the ones that are files

    files = glob.glob(join(in_path, "**/*"), recursive=True)
    files = [f for f in files if os.path.isfile(f)]
    

提交回复
热议问题