Python实现标题有固定格式字符的文件分类保存
公司有个服务器负责电话录音,文件格式类似:年月日+7个字符+0.wav。
最后一个数字代表录音的不同的电话号码。一共有7个号码被录音了。所以文件名最后一位就是0-7。
本来工作人员每周需要从录音服务器里拷贝出来几百个录音文件然后手工分类放入对应电话号码的文件夹内。我用Python做了个小程序,实现了根据文件名最后一位的数字自动创建对应电话号码文件夹,把该号码对应的所有录音文件放入该文件夹内,实现自动分类的功能。
先上代码:
import glob,datetime,os,shutil
from time import strftime
now=datetime.datetime.now()
print(now.strftime("%Y%m%d"))
print(now.strftime("%Y%m"))
if __name__ == '__main__':
file_compare_dir = "D:\\Python\\RECORD\\recvoc\\"
file_out_dir = "D:\\Python\\RECORD\\File_out\\9360\\"
if not os.path.exists(file_out_dir):
os.mkdir(file_out_dir)
id_file = file_compare_dir + now.strftime("%Y%m") + "\\" +'*0.wav'
print(id_file)
len_file = len(glob.glob(id_file))
print("共找到")
print (len_file)
for file in glob.glob(id_file):
print(file)
shutil.copy(os.path.join(file),os.path.join(file_out_dir))
file_compare_dir = "D:\\Python\\RECORD\\recvoc\\"
file_out_dir = "D:\\Python\\RECORD\\File_out\\9361\\"
if not os.path.exists(file_out_dir):
os.mkdir(file_out_dir)
id_file = file_compare_dir + now.strftime("%Y%m") + "\\" +'*1.wav'
len_file = len(glob.glob(id_file))
print("共找到")
print (len_file)
for file in glob.glob(id_file):
print(file)
shutil.copy(os.path.join(file),os.path.join(file_out_dir))
file_compare_dir = "D:\\Python\\RECORD\\recvoc\\"
file_out_dir = "D:\\Python\\RECORD\\File_out\\9362\\"
if not os.path.exists(file_out_dir):
os.mkdir(file_out_dir)
id_file = file_compare_dir + now.strftime("%Y%m") + "\\" +'*2.wav'
len_file = len(glob.glob(id_file))
print("共找到")
print (len_file)
for file in glob.glob(id_file):
print(file)
shutil.copy(os.path.join(file),os.path.join(file_out_dir))
file_compare_dir = "D:\\Python\\RECORD\\recvoc\\"
file_out_dir = "D:\\Python\\RECORD\\File_out\\9598\\"
if not os.path.exists(file_out_dir):
os.mkdir(file_out_dir)
id_file = file_compare_dir + now.strftime("%Y%m") + "\\" +'*3.wav'
len_file = len(glob.glob(id_file))
print("共找到")
print (len_file)
for file in glob.glob(id_file):
print(file)
shutil.copy(os.path.join(file),os.path.join(file_out_dir))
file_compare_dir = "D:\\Python\\RECORD\\recvoc\\"
file_out_dir = "D:\\Python\\RECORD\\File_out\\9240\\"
if not os.path.exists(file_out_dir):
os.mkdir(file_out_dir)
id_file = file_compare_dir + now.strftime("%Y%m") + "\\" +'*4.wav'
len_file = len(glob.glob(id_file))
print("共找到")
print (len_file)
for file in glob.glob(id_file):
print(file)
shutil.copy(os.path.join(file),os.path.join(file_out_dir))
file_compare_dir = "D:\\Python\\RECORD\\recvoc\\"
file_out_dir = "D:\\Python\\RECORD\\File_out\\9280\\"
if not os.path.exists(file_out_dir):
os.mkdir(file_out_dir)
id_file = file_compare_dir + now.strftime("%Y%m") + "\\" +'*5.wav'
len_file = len(glob.glob(id_file))
print("共找到")
print (len_file)
for file in glob.glob(id_file):
print(file)
shutil.copy(os.path.join(file),os.path.join(file_out_dir))
file_compare_dir = "D:\\Python\\RECORD\\recvoc\\"
file_out_dir = "D:\\Python\\RECORD\\File_out\\9290\\"
if not os.path.exists(file_out_dir):
os.mkdir(file_out_dir)
id_file = file_compare_dir + now.strftime("%Y%m") + "\\" +'*6.wav'
len_file = len(glob.glob(id_file))
print("共找到")
print (len_file)
for file in glob.glob(id_file):
print(file)
shutil.copy(os.path.join(file),os.path.join(file_out_dir))
file_compare_dir = "D:\\Python\\RECORD\\recvoc\\"
file_out_dir = "D:\\Python\\RECORD\\File_out\\9360\\"
if not os.path.exists(file_out_dir):
os.mkdir(file_out_dir)
id_file = file_compare_dir + now.strftime("%Y%m") + "\\" +'*7.wav'
len_file = len(glob.glob(id_file))
print("共找到")
print (len_file)
for file in glob.glob(id_file):
print(file)
shutil.copy(os.path.join(file),os.path.join(file_out_dir))
file_compare_dir = "D:\\Python\\RECORD\\recvoc\\"
file_out_dir = "D:\\Python\\RECORD\\File_out\\9360\\"
if not os.path.exists(file_out_dir):
os.mkdir(file_out_dir)
id_file = file_compare_dir + now.strftime("%Y%m") + "\\" +'*8.wav'
len_file = len(glob.glob(id_file))
print("共找到")
print (len_file)
for file in glob.glob(id_file):
print(file)
shutil.copy(os.path.join(file),os.path.join(file_out_dir))
程序输出
##程序运行后自动创建的文件夹
程序主要用了Python的shutil模块来实现文件复制粘贴,看看源代码就会了就不解释了。