-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScL_search_and_copy_files.py
More file actions
31 lines (26 loc) · 1.18 KB
/
ScL_search_and_copy_files.py
File metadata and controls
31 lines (26 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os
import shutil
def search_and_copy_files(search_string, destination_folder):
"""
Search for files in the current directory containing a specific string in their filenames
and copy them to a separate folder.
Args:
search_string (str): The string to search for in filenames.
destination_folder (str): The folder where matching files will be copied.
"""
# Ensure the destination folder exists
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
# Get list of all files in the current directory
files = os.listdir()
# Loop through each file in the directory
for file in files:
# Check if the file contains the search string and is a file (not a directory)
if search_string in file and os.path.isfile(file):
# Copy the file to the destination folder
shutil.copy(file, destination_folder)
print(f"Copied: {file} to {destination_folder}")
# Example usage
search_string = "ScL" # Replace with your desired search string
destination_folder = "ScL" # Replace with your desired destination folder name
search_and_copy_files(search_string, destination_folder)