-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortmusic.py
executable file
·64 lines (57 loc) · 2.03 KB
/
sortmusic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# cleans music directory folder structure. Moves music from directories with
# only a couple of files to the parent, then deletes empty dirs.
import os, commands, string, sys, shutil, glob
basepath = "/home/music"
# list directories
def GetSubDirs(dirname):
if os.path.isdir(dirname):
dirList = os.listdir(dirname)
subDirs = []
for item in dirList:
fullpath = os.path.join(dirname, item)
if os.path.isdir(fullpath):
subDirs = subDirs + [fullpath]
return subDirs
else:
msg = dirname + " is not a directory, can't have subdirectories"
return msg
def GetFiles(dirname):
if os.path.isdir(dirname):
dirList = os.listdir(dirname)
files = []
for item in dirList:
fullpath = os.path.join(dirname, item)
if not os.path.isdir(fullpath):
files = files + [fullpath]
return files
else:
msg = dirname + " is not a directory, so it doesn't contain files"
return msg
def RecurseSubdir(dirname):
subdirs = GetSubDirs(dirname)
directorylist = []
for directory in subdirs:
sublist = RecurseSubdir(directory)
directorylist = directorylist + sublist
return directorylist
def MoveFewToParent(dirname):
movedNum = 0
for (path,dirs,files) in os.walk(dirname):
if len(dirs) == 0 :
if len(files) < 2 :
for file in files:
oldpath = os.path.join(path, file)
newpath = os.path.join(os.path.split(path)[0], file)
shutil.move( oldpath , newpath)
movedNum += 1
return "moved " + str(movedNum) + " files."
def DeleteEmptySubDirs(dirname):
deletedNum = 0
for (path,dirs,files) in os.walk(dirname):
if len(dirs) == 0 :
if len(files) == 0:
os.rmdir(path)
deletedNum = deletedNum + 1
return "deleted " + str(deletedNum) + " empty directories."