Using Jupyter notebook, I created a Python code that would copy files with a keyword from one directory to a new one created in the script (i.e. files were kept in: D:/Main_Folder/SubFolder1/SubFolder2, and the new directory would be D:/Main_Folder/SubFolder1/SubFolder2/NewFolder).
I used the filedialog.askdirectory()
to get the directory of the original files, and the new directory would be created within that one. Python did it all wrong and created the new directory as follows:
D:/Main_Folder/SubFolder1/SubFolder2\NewFolder
Basically created the new folder but used a backslash instead. Is there any way at all to recover the files? I used the shutil.copyfile so I'm not entirely sure why they got deleted form the original folder.
import os
import shutil
from tkinter import Tk
from tkinter import filedialog
root = Tk()
root.withdraw()
folder_path = filedialog.askdirectory()
new_directory = os.path.join(folder_path, 'Red')
if not os.path.exists(new_directory):
os.makedirs(new_directory)
keyword = 'Red_'
for root_folder, _, filenames in os.walk(folder_path):
for filename in filenames:
if keyword in filename and filename.endswith(".tif"):
source = os.path.join(root_folder, filename)
destination_folder_name = os.path.basename(os.path.dirname(source))
destination_folder = os.path.join(new_directory, destination_folder_name)
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
destination = os.path.join(destination_folder, filename)
shutil.copyfile(source, destination)
print("Files copied to new folder!")