thurask

Untitled

Feb 24th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. import os
  2.  
  3. def indexer(folder):
  4.     for idx,itm in enumerate(folder):
  5.         print("{}: {}".format(idx, itm))  # print contents plus indices
  6.     inp = int(input("Choice [0-{}]: ".format(len(folder))))  # choose index
  7.     return inp
  8.  
  9. def songindex(folder):  # index chooser for MP3/M4A files
  10.     files = [os.path.abspath(x) for x in os.listdir(folder) if x.endswith((".m4a", ".mp3"))]  # only music files, return absolute paths
  11.     names = [os.path.basename(x) for x in files]  # get just filenames from absolute paths
  12.     idx = indexer(names)  # get selected index
  13.     return files[idx]
  14.  
  15. def dirindex(folder):  # index chooser for directories
  16.     folders = [os.path.abspath(x) for x in os.listdir(folder) if os.path.isdir(x)]  # only folders, return absolute paths
  17.     names = [os.path.basename(x) for x in folders]  # get last component of folder name (i.e. artist/album)
  18.     idx = indexer(names)  # get selected index
  19.     return folders[idx]
  20.  
  21. music_dir = os.path.join(os.path.expanduser("~"), "Music")  # C:\users\<yournamehere>\Music
  22. # Alternatively:
  23. # music_dir = os.path.join(os.path.expanduser("~"), "Music", "iTunes", "iTunes Media", "Music")
  24. os.chdir(music_dir)
  25. print("Please choose an artist")
  26. artist = dirindex(music_dir)
  27. os.chdir(artist)
  28. print("\nPlease choose an album")
  29. album = dirindex(artist)
  30. os.chdir(album)
  31. print("\nPlease choose a song")
  32. song = songindex(album)
  33. print("\nNow playing: {}".format(os.path.basename(song)))
  34. os.startfile(song)
Add Comment
Please, Sign In to add comment