-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_music.py
97 lines (68 loc) · 2.21 KB
/
list_music.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from os import walk, name as OS_NAME
from re import sub
from time import time
from codecs import open as codecsOpen
TOP_DIR = 'C:\\Users\\Edvin\\Music'
SEP = ' '
ARTIST_COUNT = 0
ALBUM_COUNT = 0
def writeLineInFile(path, dat):
global ARTIST_COUNT
global ALBUM_COUNT
depth = path.count('\\')
if depth > 0:
line = sub('[^\\\\]*\\\\', '', path, count=depth)
else:
print(' Error in calculating path depth. Exiting.')
dat.close()
exit()
if depth == 1:
line = '- ' + line
ARTIST_COUNT += 1
elif depth == 2:
line = SEP + '- ' + line
ALBUM_COUNT += 1
else:
if userOtherDirs == 1:
line = SEP * (depth-1) + '- ' + line
else:
return
dat.write(line + '\n')
return
def dirToFile():
firstDir = True
topDirLen = len(TOP_DIR)
if userFileFormat == 1:
dat = codecsOpen('music_list.txt', 'w', encoding='utf-8')
elif userFileFormat == 2:
dat = codecsOpen('music_list.md', 'w', encoding='utf-8')
'''
walk produces 3 values for every iteration: path to the dir, subdirs in that dir, files in that dir
'''
for root, dirs, files in walk(TOP_DIR):
if firstDir:
firstDir = False
continue
else:
path = root[topDirLen:] # remove the TOP_DIR substring
writeLineInFile(path, dat)
dat.write('\n----------\n\n')
dat.write('- No. of artists: **' + str(ARTIST_COUNT) + '**\n')
dat.write('- No. of albums: **' + str(ALBUM_COUNT) + '**')
dat.close()
return
if __name__ == "__main__":
if OS_NAME == 'nt': # windows
userFileFormat = int(input(' List in .txt [1] or .md [2] file? '))
userOtherDirs = int(input(' List 3+ level directories (not artists or albums) [0 or 1]? '))
startTime = time()
if (userFileFormat in [1, 2]) and (userOtherDirs in [0, 1]):
dirToFile()
else:
print(' Input error.')
endTime = time()
print(' Run time: ' + str(endTime - startTime) + ' s.')
input(' Press any key to exit.')
else:
print(' Program not supported for your OS. Exiting.')
exit()