Reputation: 769
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"))
Upvotes: 1
Views: 372
Reputation: 919
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')
Upvotes: 1