Advertisement
Guest User

recurseM3U.py

a guest
Jun 28th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf8 -*-
  3. # This script generates M3U playlists for MP3 files recursively found inside each
  4. # subdirectory. Old M3U playlists are cleaned out previously
  5. import os
  6.  
  7. # Grab all subdirectories within the current directory
  8. search_sub ="find ./* -type d"
  9.  
  10. # Loop through all found subdirs
  11. for subdir in os.popen (search_sub):
  12.   # Strip LF at the end of the directory string
  13.   subdir = subdir[:-1]
  14.   # Add trailing / if missing
  15.   if subdir[-1] != '/': subdir += '/'
  16.  
  17.   # Read all files from current directory
  18.   files = os.listdir(subdir)  
  19.  
  20.   # Generate name for new playlist including path
  21.   playlist_name = subdir + 'playlist.m3u'
  22.  
  23.   # Generate empty playliste
  24.   playlist = []  
  25.  
  26.   # Only add MP3 files to playlist
  27.   for n in files:
  28.      if (n[-3:] == 'mp3') or (n[-3:] == 'MP3'): playlist.append(n+'\n')
  29.  
  30.      # Clean out old M3U files
  31.      if (n[-3:] == 'm3u') or (n[-3:] == 'M3U'): os.remove (subdir +  n)
  32.  
  33.  
  34.   # Sort MP3 playlist
  35.   playlist.sort()
  36.  
  37.   # Save MP3 playlist to disk
  38.   f = file (playlist_name, 'w')
  39.   f.writelines (playlist)
  40.   f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement