假期回老家后比较郁闷的一件事就是没有网, 因为在学校一直使用的是酷狗的在线播放, 所有说连电脑里面里MP3音乐也没有, 由于之前我发现将酷狗酷狗音乐缓存文件后缀直接改成”mp3”就能播放, 所以需要将修改酷狗音乐的缓存文件的后缀, 对于重命名大量缓存文件, 可以使用命令行ren *.kgtemp *.mp3直接搞定, 另外一种就是编程处理了, 显然编程显得小题大做, 但是还是决定使用Python脚本处理, 因为脚本可以同时从歌词目录从将一部分歌曲的名字获取过来并设置.
下面是处理的脚本文件, Python34
import os
import shutil
patterCache = "kgtemp"
patterLyric = "krc"
suffixLen = 36
newDir = "tempMP3"
#遍历所有目录搜索酷狗音乐的目录
def searchPath(dire):
'''seal recursive function inside searchPath, ensure that most variable are locablbe'''
#files means the number of files
#unknown means the count of files with name's encode is not gbk, and can't output
#only define like as follow can be modify
print("searching...")
res = {"lyrics": None, "caches" : None}
def traverse(dire):
'''recursive function with file counter and output file's path'''
try:#give up unreadable direction
os.chdir(dire)
except Exception:
return
#traverse all files or direcions in current direction
for x in os.listdir(dire):
#derection then recursive search
if os.path.isdir(x) and x != "$RECYCLE.BIN":
traverse(os.path.abspath(x))
#find kugou cache path
elif x[-len(patterCache) : ] == patterCache:
if not res["caches"]:
res["caches"] = os.getcwd()
else:
break
#find kugou lyric path
elif x[-len(patterLyric) : ] == patterLyric:
if not res["lyrics"]:
res["lyrics"] = os.getcwd()
else:
break
os.chdir("..")
#if has find the path of both lyric and cache, then return
if res["lyrics"] and res["caches"]:
return
traverse(dire)
print("finish searching!")
return res
def modify(cacheDir, lyricDir):
'''copy all cache in new direction and rename all file, set sufix as mp3'''
#change working dir to lyric dir
os.chdir(lyricDir)
names = {}
print("copy name list from lyrics")
for x in os.listdir(): #get all lyric file name
names[x[-suffixLen : -len(patterLyric) - 1]] = x[ : -suffixLen - 1]
#change working dir
os.chdir(cacheDir)
print("check and create new direction")
if not os.path.exists("../" + newDir):
os.mkdir("../" + newDir)
print("copying...")
for item in os.listdir():
if not os.path.exists("../" + newDir + item):
try:
#copy cache to new direciont
shutil.copy(item, "../" + newDir)
core = item[ : -len(patterCache) - 1]
if core in names:#rename cache if has lyric file
os.rename("../" + newDir + item, "../" + newDir + names[core] + ".mp3")
else:#set sufix to mp3
os.rename("../" + newDir + item, "../" + newDir + core + ".mp3")
except Exception:
pass
def verify(dire):
'''traverse all files in new direction to check whether has file not change its sufix'''
print("verifying")
os.chdir(dire)
os.chdir("../" + newDir)
for item in os.listdir():
if item[-3 : ] != "mp3":
os.rename(item, item[ : -len(patterCache) - 1] + ".mp3")
##################entry######################
loc = str(input("请输入酷狗音乐的缓存磁盘:"))
while not loc.isalpha():
print(loc, "is not valid, input again:")
loc = str(input("请输入酷狗音乐的缓存磁盘:"))
loc += ":\\"
res = searchPath(loc)
print("cache dir:", res["caches"])
print("lyric dir:", res["lyrics"])
modify(res["caches"], res["lyrics"])
verify(res["caches"])
os.chdir(res["caches"])
os.chdir("../" + newDir)
print("done check mp3 file in", os.getcwd())