python运用os、shutil模块修改该文件夹内所有歌曲

因为我原有的歌曲文件比较乱,我尝试着用python整理下,比之前自己手动操作高效多了!!

学编程还是很有用的!!

import os,shutil
'''运用osshutil模块修改该文件夹内所有文件、文件夹
要点:   1.os.walk,把文件夹里面的所有文件、文件夹都 扫一遍
        2.name.split('-')[0] 分离字符串
        3.os.path.join(root, singer)合并得出路径
        4.path.strip()              去掉前后空格
        5.shutil.move(oldfile, newfile) 移动文件
        6.os.path.splitext(name)[0]     分离文件名与扩展名
        7.os.listdir(dirname)           将该文件夹内所有文件/文件夹都罗列出来list
        8.os.rmdir(dirname)             删除文件夹(只能是空的)
        9.is_chinese()                  输入的字串符含有中文,就会返回True
        10.list.count('a')              得出a元素在list中出现的次数
'''




path=r'C:\Users\Administrator\Desktop\2'

'''歌曲命名格式:歌手-歌曲名,把同一个歌手的歌曲都放在该文件夹'''
for root, dirs, files in os.walk(path, topdown=False):
    for name in files:
        try:
            singer=name.split('-')[0]      #得出歌手
            path=os.path.join(root, singer)#歌手 文件夹的路径,下面不存在时创建该文件夹
            path=path.strip()              #去掉前后空格
            if not os.path.exists(path):
                os.makedirs(path)
            oldfile=os.path.join(root, name)
            newfile=os.path.join(path, name)
            shutil.move(oldfile, newfile)  #把歌曲移动到对应歌手的文件夹
        except Exception as e:
            print(e)
            print(oldfile,'\r'+newfile)


'''把所有歌曲都拿出来放到该目录下'''
for root, dirs, files in os.walk(path, topdown=False):
    for name in files:
        oldfile=os.path.join(root, name)
        newfile=os.path.join(path, name)
        shutil.move(oldfile,newfile)


'''把 歌名-歌手 格式的歌曲反过来,变为 歌手-歌曲'''
for root, dirs, files in os.walk(path, topdown=False):
    for name in files:
        if '-周杰伦' in name:
            song=os.path.splitext(name)[0]     #分离文件名与扩展名
            singer=song.split('-')[-1]         #歌手
            songname = song.replace(singer,'').rstrip('-')#把歌手str去掉,再把后面的-去掉
            newname=singer+'-'+songname+'.mp3' #名称调换过来了
            newfile=os.path.join(root, newname)
            oldfile=os.path.join(root, name)
            shutil.move(oldfile, newfile)



'''文件改名同时移动文件,,感觉比自己剪切快得多了!!'''
path1=r'C:\Users\Administrator\Desktop\歌曲1'
for root, dirs, files in os.walk(path1, topdown=False):
    for name in files:
        songname = name.split('_')[0]+'.m4a'
        newfile=os.path.join(path, songname)
        oldfile = os.path.join(root, name)
        shutil.move(oldfile, newfile)


'''把文件夹内文件小于2个的移除到文件夹外'''
for root, dirs, files in os.walk(path, topdown=False):
    for dir in dirs:
        dirname=os.path.join(root, dir)
        if len(os.listdir(dirname))<3:
            for song in os.listdir(dirname):
                oldfile=os.path.join(dirname,song)
                newfile=os.path.join(path, song)
                shutil.move(oldfile, newfile)


'''删除空文件夹'''
path=r'C:\Users\Administrator\Desktop\2'
for root, dirs, files in os.walk(path, topdown=False):
    for dir in dirs:
        dirname=os.path.join(root, dir)
        if len(os.listdir(dirname))==0:
            os.rmdir(dirname)
        elif len(os.listdir(dirname))==3:
            print(dirname)


'''文件名中不包含中文,就放到某个文件夹'''
def is_chinese(word):
    '''输入的字串符含有中文,就会返回True'''
    for ch in word:
        if u'\u4e00' <= ch <= u'\u9fff':
            return True
    return False

for root, dirs, files in os.walk(path, topdown=False):
    for name in files:
        if root==path:
            if  not is_chinese(name):
                oldfile=os.path.join(root, name)
                newfile=os.path.join(root+'\\英文歌曲', name)
                shutil.move(oldfile, newfile)


'''文件名相同时,删除后缀是.m4a的文件'''
nosplit=[]
for root, dirs, files in os.walk(path, topdown=False):
    for name in files:
        names=os.path.splitext(name)[0]
        nosplit.append(names)

namelist=set() #set可以过滤重复
for name in nosplit:
    if nosplit.count(name)>1: #出现超过2        namelist.add(name)

for root, dirs, files in os.walk(path, topdown=False):
    for name in files:
        if os.path.splitext(name)[1]=='.m4a':
            if os.path.splitext(name)[0] in namelist:
                oldfile = os.path.join(root, name)
                print(oldfile)
歌曲文件很乱,整理了很久!

猜你喜欢

转载自blog.csdn.net/qq_38282706/article/details/80204657