Get filenames without path of a specific directory
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you want only filenames from a directory, the cleanest solution is to ask the filesystem API for directory entries and then read each entry’s name property. The key is to avoid building full paths first and stripping them later unless your language or tool gives you no better option.
Use the Directory API, Not String Slicing
Treating paths as plain strings is error-prone because separators differ across operating systems and special cases such as trailing slashes or Unicode filenames can surprise you. It is better to use a filesystem API that already understands what a filename is.
In Python, pathlib makes this very direct:
entry.name gives you only the final filename component, not the full path.
Use os.scandir for Large Directories
If performance matters, os.scandir is often a good choice because it yields directory entries with cheap metadata access:
This approach is efficient and avoids calling os.path.basename on a list of full paths you never needed in the first place.
If You Already Have Full Paths, Use a Path Helper
Sometimes the upstream code already gave you full paths. In that case, convert them with a proper path utility instead of manual string splitting:
This is a good fallback pattern, but it is still a second-best option compared with asking the directory iterator for names directly.
Shell Example
On Unix-like systems, the shell can do the same job. find is a better example than ls because it is easier to control:
%f prints only the final filename component. This is usually safer than parsing the output of ls, which is meant for humans rather than stable scripting.
Decide Whether You Want Files, Directories, or Both
A directory listing can include regular files, directories, symbolic links, sockets, and more. The question “get filenames” often really means “get the names of regular files only,” but not always.
That is why filtering matters. In the Python examples above, entry.is_file() excludes directories. If you want every entry name instead, drop that condition:
The correct answer depends on what the caller means by “filenames.”
Hidden Files and Sorting
Many APIs include hidden files by default. If your use case should exclude names beginning with a dot, add that explicitly:
Sorting is another separate concern. Filesystem iteration order is not guaranteed to be alphabetic, so sort the result if stable ordering matters.
Common Pitfalls
The most common mistake is parsing paths manually with string splitting. That works until a path format, platform, or edge case differs from what you assumed.
Another pitfall is forgetting to filter entry types. Directory APIs can return more than regular files, so be explicit about whether directories and symlinks should be included.
It is also easy to assume the returned order is stable. Many directory iteration APIs do not guarantee sort order, so call sorted if the output needs to be predictable.
Finally, avoid parsing ls output in scripts. Purpose-built filesystem APIs and structured command output are safer and easier to maintain.
Summary
- Use a filesystem API such as
pathliboros.scandirinstead of string slicing. - Read the entry name directly when iterating a directory.
- Use
os.path.basenameonly when full paths already exist. - Filter for files, directories, or visible entries explicitly based on the real requirement.
- Sort the result yourself if the caller needs a stable order.

