Context is thus: I’m doing a data science project to investigate some UK national police data.
I have a folder containing ~4500 excel files – see photo below.
I wish to move the ones which have ‘street’ in their name to a seperate folder but am scratching my head as to what I’m doing wrong. I also would like to know how I can select the data from just 2024, say, to avoid the dataframe taking forever to load.
I have tried an if statement to try and check for the string ‘street’ and have also tried checking if the end of the file name contains the string as well.
The folder names are:
Folder with the data in is
C:UsersNathanDocumentsData Science ProjectsPolice DatasetPolice DatasetStop and Search
Folder I want to move it to
C:UsersNathanDocumentsData Science ProjectsPolice DatasetPolice DatasetStreet
The folder:
My code:
import os
import shutil
def move_excel_files(master_source_dirr, destination_directory_stop_and_search):
# Ensure the source directory exists
if not os.path.exists(master_source_dirr):
print(f"Source directory does not exist: {master_source_dirr}")
return
for folder in os.listdir(master_source_dirr):
folder_path = os.path.join(master_source_dirr, folder)
print(f"Checking folder: {folder_path}")
if os.path.isdir(folder_path):
for root, dirs, files in os.walk(folder_path):
print(f"Searching in directory: {root}")
for file in files:
print(f"Found file: {file}")
# Checking if the file name contains "street"
#if "street.csv" in file:
if file.endswith("street.csv"):
print(f"File contains 'street.csv': {file}")
source_file = os.path.join(root, file)
destination_file = os.path.join(destination_directory_stop_and_search, file)
try:
print(f"Moving {source_file} to {destination_file}")
shutil.move(source_file, destination_file)
print(f"Successfully moved: {source_file} to {destination_file}")
except Exception as e:
print(f"Error moving {source_file}: {e}")
else:
print(f"File does not contain 'street': {file}")
# Specify the master source directory containing all the files
master_source_dirr = r'C:UsersNathanDocumentsData Science ProjectsPolice DatasetPolice DatasetStop and Search'
# Specify the destination directory where all files containing "street" will be moved
destination_directory_stop_and_search = r'C:UsersNathanDocumentsData Science ProjectsPolice DatasetPolice DatasetStreet'
# Create the destination directory if it doesn't exist
os.makedirs(destination_directory_stop_and_search, exist_ok=True)
# Call the function to move files into the destination folder
move_excel_files(master_source_dirr, destination_directory_stop_and_search)
print("Done")