Skip to content

Commit e1682a1

Browse files
committed
Add music-organizer.
1 parent aa4d045 commit e1682a1

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
A collection of simple Python scripts.
33

44
## Listings
5+
### Python 2.7
6+
+ `music-organizer.py`. Organizes my music directory.
57
### Python 3
68
+ [EvalExpr.py][eval-post].
79
A module to evaluate a mathematical expression using Python's AST.

python2.7/music-organizer.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python2.7
2+
3+
import os
4+
import re
5+
import sys
6+
from mutagen.easyid3 import EasyID3
7+
8+
replaceChars = (
9+
(" ", "-"),
10+
("(", ""),
11+
(")", ""),
12+
(",", ""),
13+
(".", ""),
14+
("'", ""),
15+
("?", "")
16+
)
17+
def toNeat(s):
18+
s = s.lower()
19+
for r in replaceChars: s = s.replace(r[0], r[1])
20+
search = re.search("[^a-z\-]", s)
21+
if search:
22+
print("Error: Unrecognized character in '" + s + "'")
23+
sys.exit(-42)
24+
return s
25+
26+
for dirname, dirnames, filenames in os.walk('.'):
27+
for subdirname in dirnames:
28+
print("subdir:" + str(subdirname))
29+
for filename in filenames:
30+
fullPath = os.path.join(dirname, filename)
31+
print("file: " + str(fullPath))
32+
audio = EasyID3(fullPath)
33+
title = audio['title'][0].decode()
34+
print(" title: " + title)
35+
36+
neatTitle = toNeat(title)
37+
print(" neat-title: " + neatTitle)
38+
39+
ext = os.path.splitext(filename)[1]
40+
newFullPath = os.path.join(dirname, neatTitle + ext)
41+
print(" newFullPath: " + newFullPath)
42+
43+
if newFullPath != fullPath:
44+
if os.path.isfile(newFullPath):
45+
print("Error: File exists: '" + newFullPath + "'")
46+
sys.exit(-42)
47+
48+
os.rename(fullPath, newFullPath)
49+
50+
print("\nComplete!")

0 commit comments

Comments
 (0)