Moving specific excel files from one folder to another based on the name of the file using Python

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")

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật