Hello coders!! In this article, we will understand how to get a filename from a given path in python. We shall discuss its coding in detail, explaining the modules and the functions used for coding. So, without wasting any time, let’s get straight into the topic.
What is the Path?
As the name suggests, a path indicates the unique location in any directory, starting from the root. It points to a specific file system location by following the directory tree hierarchy expressed in a character sequence, separated by a delimiting character.
A path identifies a file in the system. The path starts from the root node. For example, the file ‘report’ in Solaris Os will be described as:
In Microsoft Windows,
report will be described as:
Python Program to Get Filename from Path:
name = os.path.basename("path/to/file/sample.txt") print(name)
- os is a module available in python that allows functions to interact with the operating system. It is a part of python’s standard utility module.
- os.path is a submodule present in the os module. It is used for different pathname manipulation.
- os.path.basename() is a method used to extract the base name in the given path. This function internally uses os.path.split(), which basically splits the given path into pairs(head and tail). The os.path.basename() method returns the tail part of the path, which essentially contains the filename.
path of the file whose name needs to be extracted.
A string, representing the base name of the file in the specified path.
Python Program to Get Filename from Path Without Extension
import os name = os.path.basename("path/to/file/sample.txt") print(name) print(os.path.splitext(name))
os.path.splitext() method in python is a function available in the os.path module. It basically splits the path into two parts:
- root: everything other than the extension part
- ext: extension part
Path of that needs to be split from the extension part
A list, containing the root part and the extension part of the file.
In this code, using the os.path.basename() method, we first extracted the filename of the specified path. Then using the os.path.splitext() method, we split that filename and printed only the root part, i.e., the file’s name without extension.
Python Program to Get Filename from Path without OS
import ntpath file = "path/to/file/sample.txt" print ("Directory: ", ntpath.dirname(file)) print ("File name: ", ntpath.basename(file))
Directory: path/to/file File name: sample.txt
ntpath is the implementation of os.path on Win32 and Win64 platforms.
Python Program to get the Filename from a Path and Save it in Current Path
import os name = os.path.basename("path/to/file/sample.txt") path = os.getcwd() print(os.path.join(path, name))
os.path.join() is a method in the os.path module of python used to join one or more components of path.
- path: path to which u want to concatenate
- *path: path components that need to be concatenated
Return Value :
A string representing the concatenated path components
- [Best] Ways to Delete a File in Python
- Python is Not Recognized as an Internal or External Command
- How to Calculate Square Root in Python
- Implementing Dijkstra’s Algorithm in Python
- Numpy Square Root | Usecase Evaluation of Math Toolkit
So, this is how one can get a filename from the path in python. It is a relatively easy process. A single function can do the task.