How do I read the number of files in a folder using Python? -
how read number of files in specific folder using python? example code awesome!
to count files , directories non-recursively can use os.listdir , take length.
to count files , directories recursively can use os.walk iterate on files , subdirectories in directory.
if want count files not directories can use os.listdir , os.path.file check if each entry file:
import os.path path = '.' num_files = len([f f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]) or alternatively using generator:
num_files = sum(os.path.isfile(os.path.join(path, f)) f in os.listdir(path)) or can use os.walk follows:
len(os.walk(path).next()[2]) i found of these ideas this thread.
Comments
Post a Comment