import os import shutil def delete_trajectory_files_and_dirs(root_dir): """ 删除指定目录下的所有 trajectory 子目录、trajectory.png 文件以及 MP4 文件。 :param root_dir: 目标根目录 """ for dirpath, dirnames, filenames in os.walk(root_dir, topdown=False): # 删除 trajectory 子目录 for dirname in dirnames: if dirname == "trajectory": dir_to_delete = os.path.join(dirpath, dirname) try: shutil.rmtree(dir_to_delete) print(f"Deleted directory: {dir_to_delete}") except Exception as e: print(f"Failed to delete directory {dir_to_delete}: {e}") # 删除 trajectory.png 文件和 MP4 文件 for filename in filenames: if filename.endswith(".png") and "trajectory" in filename: file_to_delete = os.path.join(dirpath, filename) try: os.remove(file_to_delete) print(f"Deleted file: {file_to_delete}") except Exception as e: print(f"Failed to delete file {file_to_delete}: {e}") elif filename.endswith(".mp4"): file_to_delete = os.path.join(dirpath, filename) try: os.remove(file_to_delete) print(f"Deleted file: {file_to_delete}") except Exception as e: print(f"Failed to delete file {file_to_delete}: {e}") if __name__ == "__main__": target_dir = "/home/lc/data_center/gift/trace_subimgs/actual_test/commodity" delete_trajectory_files_and_dirs(target_dir)