1

Ok, here's the scenerio: I'm making a Windows 7 program that searches for all the music files in a directory and checks the user's input against that list so it can play the song that the user selects. Because the tracks' names are often numbered like this for example: '01 name.mp3', I want to remove the '01' part from a new list and have the program record the '01', so it can recognize it from the new list and launch the file. In other words, I want the program to make a new list of filenames without the numbers, and let the program recognize the file and launch it by also knowing the number. Is this possible? (Tell me if this doesn't make any sense.) Also, Here is some of my code:

def GetMediaFName(source, name):
            for key, val in source.iteritems():
                if val == name:
                    return key

newList = {}     
media_list = []
for dirpath, dirnames, filenames in os.walk(music_dir) and os.walk(alt_music_dir1):
    for filename in [f for f in filenames if f.endswith(".m4a") or f.endswith(".mp3") or f.endswith(".mp4")]:

        media_list.append(os.path.join(dirpath, filename))



        media_from_menu = menu[5:]
        print(media_from_menu)
        media_to_search1 = media_from_menu + ".mp3"
        media_to_search2 = media_from_menu + ".wav"
        media_to_search3 = media_from_menu + ".m4a"
        media_to_search4 = media_from_menu + ".mp4"

        for i in media_list:
            spl = i.split('\\')[-1]
            if spl is not None:
                try:
                    tmp = re.findall(r'\d+',spl.split('.')[0][0])
                    newList.update({i:i.replace(tmp,"").strip()})
                except Exception:
                    newList.update({i:spl})

        itms = newList.keys()

            for i in files:
                tmp = re.findall(r'\d+',i.split('.')[0][0])
                newList.update({i:i.replace(tmp,"").strip()})
            print(newList)

        print(GetMediaFName(newList, media_from_menu + ".mp3"))
Luke Dinkler
  • 731
  • 5
  • 16
  • 36
  • Sory, i dont think that you were very clear about the part of the "01". You want to remove the "01" from the file name, or want to just ignore that number on the search/read of the file name? – lealhugui Nov 28 '14 at 19:29
  • Sorry, i'll edit it! – Luke Dinkler Nov 28 '14 at 19:30
  • With reference to your example "01 name.mp3", do you wont to show the user the name "name.mp3" or just "name"? Further, do all the music files have the same extension? – gboffi Nov 28 '14 at 21:31
  • The user is basically inputting the name of the song that the program will play. The program should match the name against filenames in a directory. Obviously, the user (or the program) isn't going to know the name of the track (01). – Luke Dinkler Nov 28 '14 at 22:13

1 Answers1

1

I am not sure if i understand it correctly, but you want to keep track of original file names while showing a different name ( let say track name without index number ) to user.

I think this might give you some idea about it . Are you going to use some GUI libraries ?

import re

def getFileName(source,name):
    for key,val in source.iteritems():
        if val == name:
            return key

names = ['01 name_a.mp3','02 name_b.mp3','03 name_c.mp3','04 name_d.mp3']

newList = {}

for i in names:
    tmp = re.findall(r'\d+',i.split('.')[0])[0]
    newList.update({i:i.replace(tmp,"").strip()})

print newList

# If names are not the same, but you run in trouble if all of tracks are 01 name.mp3 , 02 name.mp3 and so on

print getFileName(newList,'name_a.mp3')

# Another possible way ! get the index of element user has clicked on and pass it to a list of original file names
user_selected = 2
itms = newList.keys()
print itms[user_selected]

EDIT:

To find Mp3s in a certain path including files in its subdirectories:

import os
import os.path
import re

def getFileName(source,name):
    for key,val in source.iteritems():
        if val == name:
            return key


names = []
# From : http://stackoverflow.com/a/954522/3815839
for dirpath, dirnames, filenames in os.walk("C:\Users\test\Desktop\mp3s"):
    for filename in [f for f in filenames if f.endswith(".mp3")]:
        names.append(os.path.join(dirpath, filename))

newList = {}
for i in names:
    spl = i.split('\\')[-1]
    if spl is not None:
        try:
            tmp = re.findall(r'\d+',spl.split('.')[0])[0]
            newList.update({i:i.replace(tmp,"").strip()})
        except Exception:
            newList.update({i:spl})
itms = newList.keys()
print newList

print getFileName(newList,'name_a.mp3')
mitghi
  • 889
  • 7
  • 20
  • Thanks for the reply! I got an error here: newList.update({i:i.replace(tmp,"").strip()}) TypeError: Can't convert 'list' object to str implicitly – Luke Dinkler Nov 28 '14 at 22:53
  • Can you please show me the code you are trying to run ? – mitghi Nov 29 '14 at 08:25
  • It's a rather large program! What portion would you like to see? – Luke Dinkler Nov 29 '14 at 23:40
  • I want to see the names list. Do you get this error when you try to run exactly the code above ? or did you fed names list with different names ? – mitghi Nov 30 '14 at 10:14
  • Unfortunately, I am currently trying to get the names list of files from [for root, dirs, files in os.walk()] but apparently it does't return an actual list of files. Could you possibly give me an idea how to get a list of files with a file extension in a directory and its subdirs? – Luke Dinkler Nov 30 '14 at 18:11
  • Sure, I add it to my previous post . – mitghi Dec 01 '14 at 08:45
  • Ok, thanks! I ran this and it prints the dictionary. I think I see what you are trying to do but the entries still have the '01' part. Also I get this error for the GetFileName fuction: for key, val in source.iteritems(): AttributeError: 'dict' object has no attribute 'iteritems' – Luke Dinkler Dec 02 '14 at 20:05
  • idek what that means. – Luke Dinkler Dec 03 '14 at 22:08
  • Does it mean Python 3? I'm still working on the problem by the way. – Luke Dinkler Dec 20 '14 at 16:16