C:\Users\guancz\Documents\辅助材料\555555\222\123.jpg 使用python将上面路径分割为 路径,文件名,文件名后缀
时间: 2024-03-10 21:42:47 浏览: 133
可以使用Python的os模块中的函数来分割路径、文件名和文件名后缀。下面是一个示例代码:
```python
import os
path = r'C:\Users\guancz\Documents\辅助材料\555555\222\123.jpg'
# 分割路径和文件名
dir_path, file_name = os.path.split(path)
# 分割文件名和文件名后缀
file_name, file_ext = os.path.splitext(file_name)
print("路径:", dir_path)
print("文件名:", file_name)
print("文件名后缀:", file_ext)
```
输出结果为:
```
路径: C:\Users\guancz\Documents\辅助材料\555555\222
文件名: 123
文件名后缀: .jpg
```
相关问题
python路径加上文件名
在Python中,你可以使用`os.path.join()`函数来组合路径和文件名。这个函数会自动处理不同操作系统之间的路径分隔符,使得你的代码更通用。例如,如果你有一个目录路径`dir_path`和一个文件名`file_name`,你可以这样做:
```python
import os
dir_path = "/Users/your_username/Documents"
file_name = "example.txt"
full_path = os.path.join(dir_path, file_name)
print(full_path)
```
这将输出类似于`"/Users/your_username/Documents/example.txt"`的完整路径。如果路径已经包含分隔符,`os.path.join()`不会添加额外的分隔符。
python 根据绝对路径取文件名
可以使用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`函数获取文件名,最后将文件名打印出来。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)