上代码:
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)