How to get only files in directory?

This question already has an answer here:

  • How do I list all files of a directory? 31 answers

  • You can use os.path.isfile method:

    import os
    from os import path
    files = [f for f in os.listdir(dirToScreens) if path.isfile(f)]
    

    Or if you feel functional :D

    files = filter(path.isfile, os.listdir(dirToScreens))
    

    “如果你需要一个文件名列表,它们都有一个特定的扩展名,前缀或者中间的任何常用字符串,使用glob而不是编写代码来自己扫描目录内容”

    import os
    import glob
    
    [name for name in glob.glob(os.path.join(path,'*.*')) if os.path.isfile(os.path.join(path,name))]
    
    链接地址: http://www.djcxy.com/p/20038.html

    上一篇: Python:读取所有目录中的所有文件

    下一篇: 如何获取目录中的文件?