Os.path.basename() method in pythonThe 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. Example 1: Output: 'myfile.txt' Example 2: Let's take another example with an empty string: Output: "" 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: |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India