脚本需求:将统一格式的音频文件以音频内容命名文件夹,并将音频放入。
我的笨蛋做法:先将每个音频重命名,音频匹配文本我是按照暴力遍历来的。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
# coding=gbk
import pandas as pd
import shutil
filePath = r'D:send-ddlcopy3'
df = pd.read_excel('D:send-ddl核心说法核心说法.xlsx')
filesname = os.listdir(filePath)
# print(len(filesname))
# 读取多列数据(这里是第1列和第2列)
data = df.iloc[:, [0, 1]].values
def comparison(a, b):
return (a == b)
for filename in filesname:
oldF = os.path.join(filePath, filename)
eachlog = filename[0:11]
print("{} is each".format(eachlog))
for i in range(len(data)):
comdata = data[i][0]
comdata = comdata[0:11]
# print("{} is com".format(comdata[7:11]))
#若文件名与列表文件名相同,则更新名字
if comparison(eachlog, comdata):
newname = data[i][1] + '_' + eachlog + ".wav"
newF = os.path.join(filePath, newname)
print("{} is old".format(oldF))
print("{} is new".format(newF))
os.rename(oldF, newF)
重命名后文件类似于:早上好_acm0001abcd@lu038973
然后将这些文件按照文件名分割,放入对应文件夹,思路为:首先切割文件名,用文件名作为新建文件夹的名字,要检查文件夹是否已经存在。
#尝试根据前缀名分类
# Python 根据文件名新建文件夹并移动到文件夹里面据文件名新建文件夹并移动到文件夹里面
import os
import shutil
def main():
path = r"D:send-ddlclassified-voice"
newPath = r"D:send-ddlclass%s"
for (root, dirs, files) in os.walk(path):
for filename in files:
singleFile = os.path.join(root, filename)
res = os.path.splitext(filename)
newFileDirs = newPath % (res[0].split('_')[0]);
# 文件不存在则新建文件
if not os.path.exists(newFileDirs):
os.mkdir(newFileDirs)
# 将当前文件移动到新建的文件夹里面
# os.rename(singleFile, newF)
shutil.move(singleFile, newFileDirs + "\" + res[0].split('_')[1] + '.wav')
print(filename+'转移成功!')
# index += 1
pass
if __name__ == '__main__':
main()
图片也适用,略加修改即可使用



