In python How do I list all files of a directory ?
Let us discuss about the solution:
In pyhton , os.listdir() will get you everything that’s in a directory – files and directories.
If we want just files, we could either filter this down using os.path:
from os import listdir from os.path import isfile, join onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
or else we could use os.walk() which will yield two lists for each directory it visits – splitting into files and dirs for us. If we only want the top directory you can just break the first time it yields
from os import walk f =  for (dirpath, dirnames, filenames) in walk(mypath): f.extend(filenames) break
Finally, as that example shows, adding one list to another we can either use .extend() or
>>> q = [1, 2, 3] >>> w = [4, 5, 6] >>> q = q + w >>> q [1, 2, 3, 4, 5, 6]
.extend() is mostly preferable