How to List All Files in Directory with Python?

python

os.listdir lists all files and directories in a given directory

import os
list_of_files_and_directories = os.listdir("my-path")

Further, use os.path.isfile to filter out only files and ignore the directories

import os
import os.path

dir = "my-path"
full_list = os.listdir(dir)
only_files = []
for filename in full_list:
    if os.path.isfile(os.path.join(dir, filename)):
        only_files.append(filename)

Latest Questions

python How to Fix ""zsh: command not found: python" Error on MacOS X? python How to Fix "xlrd.biffh.XLRDError: Excel xlsx file; not supported" Error in Pandas? python How to Remove All Whitespace From a Python String?