python os.basename
时间: 2023-10-04 22:03:35 浏览: 64
os.path.basename() is a function in Python's built-in os module that returns the base name of a path. The base name is the last part of the path, which is separated from the rest of the path by the directory separator.
Example:
```python
import os
path = '/home/user/documents/example.txt'
basename = os.path.basename(path)
print(basename)
```
Output:
```
example.txt
```
In the above example, os.path.basename() returns the base name of the path `/home/user/documents/example.txt` which is `example.txt`.
相关问题
os.path.basename
os.path.basename() 是 Python 中的一个函数,用于获取一个路径字符串中的文件名部分。具体来说,它会返回路径字符串中最后一个目录分隔符之后的所有字符,也就是文件名部分。例如,如果给定路径字符串为 "/usr/local/bin/python",那么 os.path.basename() 函数将返回 "python"。如果路径字符串以目录分隔符结尾,那么返回值将为空字符串。
os.path.basename(path)
os.path.basename(path)函数用于返回路径中的文件名部分。在给定路径的情况下,该函数将返回该路径的最后一个组成部分作为文件名。如果路径以斜杠结尾,则返回空字符串。如果路径中没有斜杠,则返回整个路径作为文件名。如果路径为空,则返回空字符串。
举例说明:
- 对于路径'E:\\Py_a_work23\\Python_os\\a\\b',os.path.basename将返回'b'。
- 对于路径'E:\\Py_a_work23\\Python_os\\a\\b/',os.path.basename将返回空字符串。
- 对于路径'ab.txt',os.path.basename将返回'ab.txt'。
- 对于路径'',os.path.basename将返回空字符串。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)