I have a series of files housed inside another file. These files refer to various kitchen dishes and are housed in a file called 'kitchen'. Attached image:
What I would like is to move all the files that have the name "meats-type_of_meat" to another folder, hosted on the same site, which will be called 'meats'. However, when using shutil.move
, I don't know why but the file named "carnes-anade" disappears and only its content appears. I add an image so that it is understood:
As you can see in the image, the file (which I have called 'meats') contains all the folders referring to the type of meat and two .txt files. These two files are the ones that were inside the "meats-anade" folder, and I don't understand why this folder has disappeared and only its files have remained when all I have done is move the folders from one place to another.
I add the code that I have used.
import re
import os
import shutil
for root, dirs, files in os.walk(root_path):
for dire in dirs:
if re.findall(r'carnes-', dire):
shutil.move(os.path.join(root_path, dire),
os.path.join(root_path, 'carnes'))
Where "root_path" refers to the main folder, that is, to 'kitchen', which is where the other food plate folders are located. What I have done is search through the function for re.findall()
all the files that have the string "meats-" and I have moved them to the "meats" folder, which is created directly once I execute the code.
Does anyone know what could be going on?
Thanks in advance.
The problem is that shutil.move() works fine on existing directories. If the destination doesn't exist, the behavior will be unpredictable , just like it happens to you.
I'll add a couple of variables to understand better, but all you have to do to solve it is check if the destination directory exists: