Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/netedit/scripts/copyElements.py
169678 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
4
# Copyright (C) 2024-2025 German Aerospace Center (DLR) and others.
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file copyElements.py
16
# @author Pablo Alvarez Lopez
17
# @date 2024-12-09
18
19
import os
20
import shutil
21
22
# Function to copy a file if a reference file exists in the folder
23
24
25
def copy_file_if_reference_exists(source_file, base_folder, reference_file):
26
# Check if the source file exists
27
if not os.path.isfile(source_file):
28
print(source_file + " doesn't exist")
29
return
30
31
# Traverse all folders and subfolders in the base folder
32
for root, dirs, files in os.walk(base_folder):
33
for dir_name in dirs:
34
destination_folder = os.path.join(root, dir_name)
35
reference_file_path = os.path.join(destination_folder, reference_file)
36
37
# Check if the reference file exists in the folder
38
if os.path.isfile(reference_file_path):
39
try:
40
# Copy the file to the current folder
41
shutil.copy(source_file, destination_folder)
42
print(f"File copied to: {destination_folder}")
43
except Exception:
44
print("File " + source_file + " cannnot be copied to " + destination_folder)
45
46
47
# Main block
48
if __name__ == "__main__":
49
50
# Path of the file to be copied
51
source_file = r"D:\meandatas.netedit"
52
53
# Path of the base folder where the file will be copied
54
base_folder = r"D:\SUMO"
55
56
# Name of the reference file that must exist in the folder
57
reference_file = "routes.netedit"
58
59
# Call the function
60
copy_file_if_reference_exists(source_file, base_folder, reference_file)
61
62