我经常想在文件名后添加一个字符串,同时保留扩展名。
定义一个函数来后置字符串似乎很麻烦。例如(使用pathlib的路径):
from pathlib import Path
def postpend(filename, string):
filepath = Path(filename)
return filepath.parent / (filepath.stem + string + filepath.suffix)
Run Code Online (Sandbox Code Playgroud)
是否有内置函数可以为我执行此操作?有没有更好的办法?