/ Published in: Python
with a given directory address, looks for musics in the directory and it's subdirectories and ask for launching them
Expand |
Embed | Plain Text
# BY: AMIR NAGHAVI # olempico@gmail.com """ this program is made for playing music tracks in a directory and it's subdirectories with user permission. it works roughly random but not at whole. you can modify it's suflst list to play and launch any program with permitted suffix you want. """ import os from re import findall from random import randint # ------------------------------------------- class shufle_launch(object): def __init__(self,add='C:\Music'): "add is directory path, self.c is counter look at run(),and lst is for temporary songs go to run() and play()" self.add=add #address self.c=0 self.lst=[] #for collecting music paths # ------------------------------------------- def search(self,add,music): # to search in musics add:directory path,music:search key for item in os.listdir(add): path=os.path.join(add,item) if os.path.isfile(path): ppath=path.split('\\') #in windows, python separates paths with"\\" if path.find(music)!=-1: print 'do u want to play %s'%(ppath[-2],ppath[-1]) f=raw_input('') if f=='y': os.startfile(path) # ---------------------------------------------------- # to undrstand easier def play(self,l): # l is the list of music paths # first review run() then play() while True: pnum=randint(0,len(l)) # play number try: # ------------- try/except block---- print l[pnum] a=raw_input('y for play or q for quit or to search enter s: ') if a=='s': m=raw_input('enter the music name: ') self.search(self.add,m) if a=='q': print 'return' exit() if a=='y': os.startfile(l[pnum]) print 'mow playing',os.path.basename(l[pnum]) l.pop(pnum) raw_input('') else: l.pop(pnum) except: # -------------- try/except block---- print "can't play",os.path.basename(l[pnum]) l.pop(pnum) pass if len(l)==0: return # ------------------------------------------------------------------------- def run(self): # literate through file paths in a directory and it's subdirectories for item in os.listdir(self.add): path=os.path.join(self.add,item) #making absolute path suflst=['mp3','wav','aac','flac','wma','ogg'] # for playing just music- you can add your suffix to it try: suf=findall('.*[.]{1}([a-zA-Z0-9]{2,4})',path)[0] #extracting suffix such as MP3 except: pass try: if os.path.isfile(path) and len(suf)>0 and (suf.lower() in suflst): self.c+=1 self.lst.append(path) if self.c%30==0: # every times that self.lst has 30 member we wood run play fun()^^^ self.play(self.lst) #and make lst emty.^^^ self.lst=[] except: pass if os.path.isdir(path): # going through subdirectories^^^ shufle_launch(path).run() # ------>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #END# # ------>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> if __name__=="__main__": shufle_launch('\Music').run()
Comments
Subscribe to comments
You need to login to post a comment.

if possible please give your suggests and opinions about my code a bit abstraction and complexity is in code is for practicing my skills in programming. merci!