The python exercise program changes the file name in batches
- 2020-04-02 13:23:07
- OfStack
# encoding:utf-8
##
# The file name as :
# Next! .mp3
##
import os,re
fs=os.listdir('xb')
for f in fs:
###### Method one: partition Get the null character
#1. Change the file name to '[' Operators are divided into 3 Part of the
#ls=f.partition('[')
#2.ls[0] Is required for the file name, so get ls[1:]
#dirtystring = ''.join(ls[1:])
#3. Began to replace
#newname=f.replace(dirtystring, '') + '.mp3')
#os.rename('xb/' + f, newname)
###### Method 2: get useless characters regularly
dirtymatch = re.search(r'[.*?]', f)
if dirtymatch:
dirtystring=dirtymatch.group(0)
newname=f.replace(dirtystring, '') + '.mp3'
os.rename('xb/' + f, newname)
# Note: can be used directly re.sub Method to replace the filename with no characters