python 分开后缀名
时间: 2023-11-03 10:01:40 浏览: 140
Python-07-使用连接更新表中某个字段数据.ev4.rar
你可以使用以下三种方式来分开文件名和扩展名:
1. 使用字符串分割再拼接的方式:
```python
for file in full_files:
file_split = file.split(".")
name = '.'.join(file_split[0 : -1])
suffix = '.' + file_split[-1]
print(f"name : {name}, suffix : {suffix}")
```
2. 使用Python自带的接口`os.path.splitext`:
```python
import os
for file in full_files:
pure_path = os.path.splitext(file)
name = pure_path[0]
suffix = pure_path[1]
print(f"name : {name}, suffix : {suffix}")
```
3. 使用`pathlib.PurePath`:
```python
import pathlib
for file in full_files:
pure_path = pathlib.PurePath(file)
name = pure_path.stem
suffix = pure_path.suffix
print(f"name : {name}, suffix : {suffix}")
```
阅读全文