python 根据绝对路径取文件名
时间: 2023-10-12 08:13:58 浏览: 54
可以使用os.path模块中的basename函数来获取绝对路径对应的文件名。
示例代码如下:
```python
import os
abs_path = '/Users/username/Documents/test.txt'
filename = os.path.basename(abs_path)
print(filename) # 输出:test.txt
```
在上面的示例代码中,先定义了一个绝对路径`abs_path`,然后使用`os.path.basename`函数获取文件名,最后将文件名打印出来。
相关问题
python中通过绝对路径获取文件名
可以使用Python的os模块中的basename函数来获取文件名。具体代码如下:
```
import os
filename = os.path.basename('/path/to/file')
print(filename)
```
其中,`/path/to/file`是要获取文件名的文件的绝对路径。函数调用后就可以获取文件名并打印出来。
python 如何在绝对路径提取文件名。
可以使用Python的os模块中的os.path.basename()函数来提取绝对路径中的文件名。示例代码如下:
```python
import os
abs_path = '/home/user/documents/example.txt'
file_name = os.path.basename(abs_path)
print(file_name) # 输出:example.txt
```
在这个例子中,我们将绝对路径存储在变量abs_path中,然后使用os.path.basename()函数提取文件名,并将结果存储在变量file_name中。最后,我们使用print()函数输出变量file_name的值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)