首页 > 解决方案 > python文件名清理

问题描述

在 python 中是否可以去除所有特殊字符和空格并用连字符替换但保留点扩展名(即文件扩展名,例如 .mp4 或 .mov 或 .txt 或 .jpg 或 .png 或 .pdf)

例如:

string = 'Special $#! characters   spaces_ 888323.mp4'

应该导致

Special-characters-spaces_-888323.mp4

我正在使用以下方法,但认为它不是正确的方法

re.sub('(\W+)','-', string).replace('-mp4','.mp4').replace('-mov','.mov')

标签: pythonregex

解决方案


If it's really file names you're dealing with, consider using os.path.splitext to temporarily detach the extension:

name, ext = os.path.splitext(string)

Now you can freely operate on name, keeping ext intact:

name = re.sub('(\W+)','-', name)

And then you just recombine:

string = name + ext

If it bothers you that this isn't a one-liner, make it into a function. I would also recommend pre-compiling the regex.


推荐阅读