os.path.basenam
时间: 2024-05-11 20:13:54 浏览: 9
e() function
The os.path.basename() function is used to get the base or the last component of a path. It returns the file or directory name at the end of the path.
Syntax:
```python
os.path.basename(path)
```
Parameter:
- path: A string representing the path.
Return Value:
- The base or last component of the path.
Example:
```python
import os
# get the base name of a file path
path = "/home/user/Documents/file.txt"
base_name = os.path.basename(path)
print(base_name) # file.txt
# get the base name of a directory path
path = "/home/user/Documents/"
base_name = os.path.basename(path)
print(base_name) # Documents
```
In the above example, we have used the os.path.basename() function to get the base name of a file path and a directory path. The function returns the last component of the path, which is the file name in the case of a file path, and the directory name in the case of a directory path.