Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/netedit/scripts/copyConfigs/script.py
193874 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2025-2026 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file script.py
15
# @author Pablo Alvarez Lopez
16
# @date 2025-12-05
17
18
import os
19
import shutil
20
import sys
21
22
23
def copy_file_to_matching_folders(root_path, target_folder_name, source_file_path):
24
"""
25
Traverses root_path, looks for folders matching target_folder_name,
26
and copies source_file_path into them.
27
"""
28
# 1. Validate inputs
29
if not os.path.isdir(root_path):
30
print(f"Error: The root path '{root_path}' is not a valid directory.")
31
sys.exit(1)
32
33
if not os.path.isfile(source_file_path):
34
print(f"Error: The file '{source_file_path}' does not exist.")
35
sys.exit(1)
36
37
print(f"Scanning '{root_path}' for folders named '{target_folder_name}'...")
38
39
copied_count = 0
40
41
# 2. Walk through the directory tree
42
for current_root, dirs, files in os.walk(root_path):
43
# Check if the target folder exists in the current directory's subdirectories
44
if target_folder_name in dirs:
45
destination_folder = os.path.join(current_root, target_folder_name)
46
47
try:
48
# 3. Copy the file
49
shutil.copy2(source_file_path, destination_folder)
50
print(f"✅ Copied file to: {destination_folder}")
51
copied_count += 1
52
except Exception as e:
53
print(f"❌ Failed to copy to {destination_folder}: {e}")
54
55
print(f"\nDone! File copied to {copied_count} location(s).")
56
57
58
def main():
59
60
# Run the function
61
copy_file_to_matching_folders("D:/Netedit_dev/tests/netedit/elements/demands", "volatile_recomputing",
62
"D:/Netedit_dev/tests/netedit/scripts/copyConfigs/netedit_A.netecfg")
63
64
65
if __name__ == "__main__":
66
main()
67
68