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, let’s get straight into the topic without wasting any time.
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:
/home/anna/report
In Microsoft Windows, report
will be described as:
C:\home\anna\report
Python Program to Get Filename from Path:
name = os.path.basename("path/to/file/sample.txt")
print(name)
Output:
sample.txt
Explanation:
- 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.
Syntax:
os.path.basename(path)
Parameter:
path of the file whose name needs to be extracted.
Return Value:
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)[0])
Output:
sample.txt
sample
Explanation:
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
Syntax:
os.path.splittext(path)
Parameter:
Path of that needs to be split from the extension part
Return Value:
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))
Output:
Directory: path/to/file
File name: sample.txt
Explanation:
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))
Output:
path/to/current/directory/sample.txt
Explanation:
os.path.join() is a method in the os.path module of python used to join one or more components of path.
Syntax:
os.path.join(path, *paths)
Parameter:
- 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
Must Read
- [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
Conclusion:
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.