py获取不带后缀的文件名称以及后缀

1. Path

from pathlib import Path

file_path = "Desktop/folder/myfile.txt"
file_name = Path(file_path).stem
extend = Path(file_path).suffix

 2.Os

import os

file_path = "Desktop/folder/myfile.txt"

file_path = os.path.splitext(file_path)[0]
file_name, extend = file_path.split("/")print(file_name)
print(extend) basename
= os.path.basename(file_path) file_name, extend = os.path.splitext(basename) print(file_name)
print(extend)

 

posted @ 2024-04-16 16:07  zjyss  阅读(84)  评论(0编辑  收藏  举报