Python created folder with \ instead of /

132 Views Asked by At

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

There are 1 best solutions below

1
ProfDFrancis On

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.