【发布时间】:2020-10-11 15:12:57
【问题描述】:
我想在扩展名之前在文件名中添加一些文本。示例:name.ext >>> name_su.ext.
我可以使用传统的 python 字符串格式来做到这一点:
filename = 'name.ext'
suffix = '_su'
print("{0}{2}{1}".format(*os.path.splitext(filename) + (suffix, )))
# This will print: name_su.ext
我想知道是否可以在一行中使用 f 字符串表示法实现相同的效果,而无需两次调用 os.path.splitext(filename)。
【问题讨论】:
-
FWIW,你甚至不需要插值。
suffix.join(os.path.splitext(filename)) -
看看this问题。