Python--音频文件分类代码

时间:2022-06-17
本文章向大家介绍Python--音频文件分类代码,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

上代码:

import os
from shutil import copy2

SOURCE = "c:\source\"

DEST = "c:\dest\"

# Iterate recursively through all files and folders under the source directory
for path, dirs, files in os.walk(SOURCE):
    # For each directory iterate over the files
    for f in files:
        # Grab the first letter of the filename
        starting_letter = f[0].upper()
        # Construct the full path of the current source file
        source_path = os.path.join(path, f)
        # Construct the destination path using the first letter of the
        # filename as the folder
        dest_path = os.path.join(DEST, starting_letter)
        # Create the destination folder if it doesn't exist
        if not os.path.isdir(dest_path):
            os.makedirs(dest_path)
        # Copy the file to the destination path + starting_letter
        copy2(source_path, dest_path)