get list of files in folder 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: get list of folders in directory python

import os 
my_list = os.listdir('My_directory')

Example 3: get file names in folder python

from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

Example 4: python get files in directory

from pathlib import Path
for txt_path in Path("/path/folder/directory").glob("*.txt"):
  print(txt_path)

Example 5: list files python

import glob
files=glob.glob(given_path)

Example 6: read all files in folder python

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