I have a folder with files named like this “artist – track XX XXX.mp3” what I’am trying to do is have the files renamed to just “artist – track.mp3”
the last part of the file will always begin one of these:
[1B, 2B, 3B, 4B, 5B, 6B, 7B, 8B, 9B, 10B, 11B, 12B, 1A, 2A, 3A, 4A, 5A, 6A, 7A, 8A, 9A, 10A, 11A, 12A]
so how do I remove the “XX XXX” and keep the rest?
import re, glob, os os.chdir("C:\Somepath") for filename in glob.glob("**/*.mp3", recursive = True): filename = re.match(r"(?P<filename>w+).*.(?P<ext>.+)", filename) filename = "{}.{}".format(filename.group('filename'), filename.group('ext')) print(filename)
which just outputs only the first artistname from the first file:
artist.mp3
artist.mp3
artist.mp3
What is the rest that I am missing here, simply can’t figure out how to solve this..
Advertisement
Answer
Your regexp group matching the filename only allows word (w
) characters, so it stops at the first space. Instead of
filename = re.match(r"(?P<filename>w+).*.(?P<ext>.+)", filename)
try
filename = re.match(r"(?P<filename>.+)s+d{1,2}[AB].*.(?P<ext>.+)", filename)
This will make the filename group match everything up to the first “XX”. The latter consists of whitespace followed by one or two digits followed by A
or B
.