0

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)

1 Answer 1

2

There is an issue with this line in your code:

    for file in file_names:

As you update file_names during your computation, the behavior of this loop is unpredictable.

Here is a working variant of your sortfiles routine (with some unnecessary code removed):

def sortfiles(file_names):
    num = 0
    tracks = []
    while file_names != []:
        num += 1
        track_test = compile_regex(num)
        matches = matchfiles(file_names, track_test)
        if matches:
            tracks.append(matches)
            file_names = removetracks(file_names, matches)

    print (tracks)
    print (file_names)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.