Search code snippets, questions, articles...

List all files of a directory using os Python

from os import listdir
from os.path import isfile, join

dirPath = "path/to/my/directory"
allFileAndFolders = listdir(dirPath)

allFiles = [f for f in allFileAndFolders if isfile(join(dirPath, f))]

You can use the os.listdir() method to list all files and folders inside a directory. You need to pass the directory path to this method and it will return you everything inside the directory.

Search Index Data (The code snippet can also be found with below search text)

os.listdir python
Was this helpful?
0 Comments
Programming Feeds
Learn something new everyday on Devsheet