python練習(xí)程序批量修改文件名
# encoding:utf-8
##
# 文件名如:
# 下吧.mp3
##
import os,re
fs=os.listdir('xb')
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('xb/' + f, newname)
######方法二:正則獲取無用字符
dirtymatch = re.search(r'\[.*?\]', f)
if dirtymatch:
dirtystring=dirtymatch.group(0)
newname=f.replace(dirtystring, '') + '.mp3'
os.rename('xb/' + f, newname)
#注意:可以直接用re.sub方法進(jìn)行正則替換掉文件名中不需要字符
相關(guān)文章
OpenCV學(xué)習(xí)記錄python實現(xiàn)連通域處理函數(shù)
這篇文章主要為大家介紹了OpenCV學(xué)習(xí)記錄python實現(xiàn)連通域處理函數(shù)cv2.connectedComponentsWithStats()和cv2.connectedComponents()的使用示例詳解2022-06-06