代码示例(Python):
import os import shutil
source_directory = '/path/to/source_directory' # 替换为源目录路径 dest_directory = '/path/to/dest_directory' # 替换为目标目录路径
for file in os.listdir(source_directory): if os.path.isfile(os.path.join(source_directory, file)): file_name, file_ext = os.path.splitext(file) dest_folder = os.path.join(dest_directory, file_name) if not os.path.exists(dest_folder): os.makedirs(dest_folder) shutil.move(os.path.join(source_directory, file), os.path.join(dest_folder, file))。
上述代码将列出源目录中的所有文件,并将它们移动到名称与其完全相同的文件夹中。此代码将扩展名与文件名分开,创建目标文件夹,如果需要的话。最后,将文件移到目标文件夹中。可以根据自己的需求更改源目录和目标目录的路径。