I use os.listdir
and it works fine, but I get sub-directories in the list also, which is not what I want: I need only files.
What function do I need to use for that?
I looked also at os.walk
and it seems to be what I want, but I'm not sure of how it works.
The listdir() method only lists all the top level files and folders inside a directory. If you want to navigate all the lower level files and folders inside your directory and subdirectories, use the walk() method from the OS module.
listdir() method in python is used to get the list of all files and directories in the specified directory. If we don't specify any directory, then list of files and directories in the current working directory will be returned. Syntax: os.listdir(path)
listdir() method returns a list of every file and folder in a directory. os. walk() function returns a list of every file in an entire file tree.
You need to filter out directories; os.listdir()
lists all names in a given path. You can use os.path.isdir()
for this:
basepath = '/path/to/directory' for fname in os.listdir(basepath): path = os.path.join(basepath, fname) if os.path.isdir(path): # skip directories continue
Note that this only filters out directories after following symlinks. fname
is not necessarily a regular file, it could also be a symlink to a file. If you need to filter out symlinks as well, you'd need to use not os.path.islink()
first.
On a modern Python version (3.5 or newer), an even better option is to use the os.scandir()
function; this produces DirEntry()
instances. In the common case, this is faster as the direntry loaded already has cached enough information to determine if an entry is a directory or not:
basepath = '/path/to/directory' for entry in os.scandir(basepath): if entry.is_dir(): # skip directories continue # use entry.path to get the full path of this entry, or use # entry.name for the base filename
You can use entry.is_file(follow_symlinks=False)
if only regular files (and not symlinks) are needed.
os.walk()
does the same work under the hood; unless you need to recurse down subdirectories, you don't need to use os.walk()
here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With