So the end goal is to sort through some audiobook tracks that are haphazardly thrown into a folder when ripped on my computer.
I am first separating all tracks with the same numbers. In the end, I know I'll need user input to decide which discs get the track numbers they don't all share, but I could make quick work of that too.
I've used regex before, so I thought, I got this, but am hung up on why the code below ceases to match tracks numbered 6 and 7, and the only difference I can see is that there is not 3 of them.
I guess I'm also not sure if I am using the for loop correctly.
All the needed code is below.
The "print tracks" command at the bottom should print all files in order, and "print file_names" should be empty, but neither of those things is true.
I get this:
[['01. Track 1 (19).mp3', '01. Track 1 (20).mp3', '01. Track 1 (21).mp3'], ['02. Track 2 (21).mp3', '02. Track 2 (20).mp3', '02. Track 2 (19).mp3'], ['03. Track 3 (21).mp3', '03. Track 3 (20).mp3', '03. Track 3 (19).mp3'], ['04. Track 4 (19).mp3', '04. Track 4 (20).mp3', '04. Track 4 (21).mp3'], ['05. Track 5 (19).mp3', '05. Track 5 (20).mp3', '05. Track 5 (21).mp3']]
['07. Track 7 (19).mp3', '06. Track 6 (20).mp3', '06. Track 6 (19).mp3']
Why isn't the second printed list completely sorted into the first?
Any help would be stupendous.
import re
file_names = ['07. Track 7 (19).mp3', '01. Track 1 (19).mp3', '01. Track 1 (20).mp3', '01. Track 1 (21).mp3', '02. Track 2 (21).mp3', '03. Track 3 (21).mp3', '02. Track 2 (20).mp3', '04. Track 4 (19).mp3', '02. Track 2 (19).mp3', '04. Track 4 (20).mp3', '05. Track 5 (19).mp3', '05. Track 5 (20).mp3', '04. Track 4 (21).mp3', '06. Track 6 (20).mp3', '03. Track 3 (20).mp3', '05. Track 5 (21).mp3', '06. Track 6 (19).mp3', '03. Track 3 (19).mp3']
def compile_regex(num):
num_str = str(num)
regex = ".*track ?" + num_str + ".*"
track_test = re.compile(regex, re.I)
return track_test
def matchfiles(file_names,track_test):
matches = []
for track in file_names:
if track_test.search(track):
matches.append(track)
return matches
def removetracks(file_names,matches):
for track in matches:
file_names.remove(track)
return (file_names)
def sortfiles(file_names):
num = 1
tracks = []
track_test = compile_regex(num)
num_tracks = len(file_names)
for file in file_names:
matches = matchfiles(file_names,track_test)
if matches:
num += 1
track_test = compile_regex(num)
tracks.append(matches)
file_names = removetracks(file_names,matches)
print tracks
print file_names
if __name__ == "__main__":
sortfiles(file_names)