How do I read the number of files in a folder using Python?

后端 未结 6 1984
醉酒成梦
醉酒成梦 2021-02-07 11:56

How do I read the number of files in a specific folder using Python? Example code would be awesome!

6条回答
  •  我在风中等你
    2021-02-07 12:35

    total = len(filter(
                lambda f: os.path.isfile(os.path.join(path_to_dir, f)),
                os.listdir(path_to_dir)))
    

    OR

    total = sum([True for f in os.listdir(path_to_dir) if os.path.isfile(os.path.join([path_to_dir, f)])
    

提交回复
热议问题