-1

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!")
halfer
  • 19,824
  • 17
  • 99
  • 186
AndSS
  • 11
  • 1
  • Welcome to stackoverflow. Please checkout the [tour], [ask] and [help/on-topic] – Marcelo Paco Mar 25 '23 at 19:06
  • This sounds like you've completely misdiagnosed the problem. What makes you think your problem has anything to do with a backslash? – user2357112 Mar 25 '23 at 19:08
  • D:/Main_Folder/SubFolder1/SubFolder2\NewFolder is not actually a valid Windows path, but much of the time, software (even the Windows shell) will treat it as the correct path, which is `D:\Main_Folder\SubFolder1\SubFolder2\NewFolder`. *However* it is possible that you accidentally attemped to create the folder `SubFolder2\NewFolder` where the `\n` is a carriage return. That is not valid in Windows and so it is possible that the folder was not created at all, and anything your code did after that was probably not what you meant. – BoarGules Mar 25 '23 at 19:09
  • 1
    Please read [Under what circumstances may I add "urgent" or other similar phrases to my question, in order to obtain faster answers?](https://meta.stackoverflow.com/q/326569/354577) (TL;DR: It's never okay.) – ChrisGPT was on strike Mar 25 '23 at 19:10
  • @user2357112 because the folder doesn't exist. If you have any other idea of where my files could be, by ALL means, tell me. But from my diagnostic view, that is the problem. – AndSS Mar 25 '23 at 19:16

1 Answers1

2

I can see you are panicked because your original files have disappeared

so I'm not entirely sure why they got deleted form the original folder

That cannot be a result of this code because, as you say, you have used copy.

It is hard to see how you could end up with random directions of slash

You have done well to use the proper methods such as os.path.join to join components of a path, rather than lazily just +"/"+ which is what I used to do before I came to Python.

You have not manually entered any slashes. You have read from the legitimate methods, and combined them using legitimate methods.

Where did you see the pathname with mixed / and \ characters?

Debugging

  • Are you sure they were there beforehand?

  • Can you search your entire directory tree for those files?

  • Any change you have manually deleted them into the Recycle bin?

  • Or dragged them into another drive or subfolder?

  • If the above fails, try bringing in (any other) TIF file into that location, and modifying the code and rerunning. Change

shutil.copyfile(source, destination)

to

print("I want to copy ",source," to ",destination)

Then rerun and see what was moved to where.


I do feel for you as I have myself managed to delete important files by mistake. But in this case, I don't think it could have been done by the code you have shown.

Good god! How did they get there?

D:/$RECYCLE.BIN\Red

Open the Recycle Bin on your desktop. Is the "Red" folder or any of the images there? Perhaps sort by "date deleted"?

If the D:/ isn't your main computer drive, go into "My Computer", and open "D:" and see if there is a "$RECYCLE.BIN". You may have to turn on the option to show hidden files and folders:

  • In File Explorer, select View > Options > Change folder and search options.
  • Select the View tab and, in Advanced settings, select Show hidden files, folders, and drives and OK.

(I am still baffled as to how you can have that mixture of forward and backward slashes, but at least they have not completely vanished.)

Once you have found them, drag them to another drive.

ProfDFrancis
  • 8,816
  • 1
  • 17
  • 26
  • I ran a Data Recovery Software and I can see that the images are in D:/$RECYCLE.BIN\Red would you know how I can access that folder? – AndSS Mar 25 '23 at 20:01