Skip to content

Commit 6aa6242

Browse files
committed
Ignore '#'. Handle no ID3 tags. Clean subdirectories using shutil.
1 parent 1027816 commit 6aa6242

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

python2.7/music-organizer.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import os
44
import re
5+
import shutil
56
import sys
67
from mutagen.easyid3 import EasyID3
78

8-
emptyChars = re.compile(r"[(),.'\\\?]")
9+
emptyChars = re.compile(r"[(),.'\\\?\#]")
910
def toNeat(s):
1011
s = s.lower().replace(" ", "-").replace("&", "and")
1112
s = emptyChars.sub("", s)
@@ -15,16 +16,20 @@ def toNeat(s):
1516
sys.exit(-42)
1617
return s
1718

19+
delete_dirs = []
1820
for dirname, dirnames, filenames in os.walk('.'):
1921
# Move all the files to the root directory.
2022
for filename in filenames:
2123
ext = os.path.splitext(filename)[1]
2224
if ext == ".mp3":
2325
fullPath = os.path.join(dirname, filename)
2426
print("file: " + str(fullPath))
25-
audio = EasyID3(fullPath)
26-
title = audio['title'][0].decode()
27-
print(" title: " + title)
27+
28+
try:
29+
audio = EasyID3(fullPath)
30+
title = audio['title'][0].decode()
31+
print(" title: " + title)
32+
except: title = None
2833

2934
if not title:
3035
print("Error: title not found for '" + filename + "'")
@@ -51,6 +56,9 @@ def toNeat(s):
5156

5257
# Delete all subdirectories.
5358
for subdirname in dirnames:
54-
os.rmdir(subdirname)
59+
delete_dirs.append(subdirname)
60+
61+
for d in delete_dirs:
62+
shutil.rmtree(d,ignore_errors=True)
5563

5664
print("\nComplete!")

0 commit comments

Comments
 (0)