1.代码
1)下面代码是移动json文件
2)看注释把.json改为自己需要移动的文件后缀名即可
import os
import shutil
def move_json_files(source_folder, destination_folder):
# 遍历源文件夹中的所有文件
for file_name in os.listdir(source_folder):
# 检查文件是否为JSON文件 可以是任何文件类型
if file_name.endswith('.json'):
# 构建源文件的完整路径
source_file = os.path.join(source_folder, file_name)
# 构建目标文件的完整路径
destination_file = os.path.join(destination_folder, file_name)
# 移动文件
shutil.move(source_file, destination_file)
print(f"Moved {file_name} to {destination_folder}")
# 指定源文件夹和目标文件夹的路径
source_folder = 'D:/python/projects/yolov5-7.0/datasets/newbitter/images/val'
destination_folder = 'D:/python/projects/yolov5-7.0/datasets/newbitter/labels/val_json'
# 调用函数进行文件移动
move_json_files(source_folder, destination_folder)
2.效果如下