- # encoding:utf-8
- import os,re
- fs=os.listdir('单田芳_贺龙传奇')
- for f in fs:
- ######方法一:partition获取无用字符
- #1.将文件名以'['符分为3部分
- #ls=f.partition('[')
- #2.ls[0]为需要文件名,因此获取ls[1:]
- #dirtystring = ''.join(ls[1:])
- #3.开始替换
- #newname=f.replace(dirtystring, '') + '.mp3')
- #os.rename('单田芳_贺龙传奇/' + f, newname)
- ######方法二:正则获取无用字符
- dirtymatch = re.search(r'\\[.*?\\]', f)
- if dirtymatch:
- dirtystring=dirtymatch.group(0)
- newname=f.replace(dirtystring, '') + '.mp3'
- os.rename('单田芳_贺龙传奇/' + f, newname)
- #注意:可以直接用re.sub方法进行正则替换掉文件名中不需要字符
- #该片段来自于http://www.codesnippet.cn/detail/200520133451.html
来源: http://www.codesnippet.cn/detail/200520133451.html