Os.path.basename() method in python
The os.path.basename() is a method in Python's os.path module that returns the base name of a file path. The base name is the final component of the path, after stripping all parent directory and extension information.
For example, if the path is /home/user/Documents/myfile.txt, the base name is myfile.txt.
Syntax for os.path.basename():
The path argument is a string representing the file path whose base name we want to retrieve. The method returns a string that contains the file path's base name. The method also returns an empty string if the path parameter is an empty string.
Let's take another example with an empty string:
os.path.basename() does not check if the file or directory exists, it only extracts the base name from the provided path.
Some additional points are as follows:
The method works on all operating systems:
The method can handle different file path formats:
The method can handle paths with or without extensions:
The method can handle directories:
Here are some more examples:
os.path.basename() returns a string object:
The method does not modify the original file path:
os.path.basename() can be used with another os.path methods:
Here is an example:
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week