how to list files in a directory python code example

Example 1: python list files in current directory

import os

files = os.listdir('.')
print(files)
for file in files:
  # do something

Example 2: python get all file names in directory

import glob
print(glob.glob("/home/adam/*.txt"))

Example 3: get list file in folder python

lstJson = [f for f in os.listdir(str(self.pathJson)) if f.endswith('.json')]
        return lstJson

Example 4: list files python

import glob
files=glob.glob(given_path)

Example 5: python read file list from directory

from shutil import copyfile
copyfile(src, dst)