python get number of files in folder code example

Example 1: python count files directory

import os
len(os.listdir(directory))

Example 2: get files in directory python

import os
files_and_directories = os.listdir("path/to/directory")

Example 3: store all files name in a folder python

import os
#this command will store all .txt files in same directories
ALL_FILES_IN_DIR = [ELEM for ELEM in os.listdir() if "txt" in ELEM]

#ALL DIRETORIES 
ALL_DIR = [ELEM for ELEM in os.listdir() if "." not in ELEM]

Example 4: python monitor directory for files count

import os.path
path = os.getenv('HOME') + '/python'
num_files = len([f for f in os.listdir(path)if os.path.isfile(os.path.join(path, f))])