Python实现标题有固定格式字符的文件分类保存

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模块来实现文件复制粘贴,看看源代码就会了就不解释了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值