I have a script that copies files from one folder to another recursively depending on extensions and it works fine until Dst is in Src. I explain:
If I send, for example, .txt files to a subfolder that is in Src, in the second round of the for cycle the code will analyze Dst and since previously it had already passed files, it will create another folder with the same name inside Dst and copy the files again. records. In other words, it clones itself. Is there any way to prevent this from happening? I've tried a continue, and it works until I copy or move files to a lower level subfolder.
As you can see in the following image the folder "folder3" copied itself and put the files inside and it is really annoying since it becomes a hodgepodge when there are more folders and files.
This is what a supposed source path looks like:
My documents
--- file.txt
--- folder (subfolder)
--- file2.txt
--- Text_files (subfolder) Dst
This is how it currently works if I want the script to copy from Src (My Documents) only the .txt files to Dst (Text_Files) recursively keeping the folder hierarchy:
My documents
--- file.txt
--- folder (subfolder)
--- file2.txt
--- Text_files (subfolder) Dst
---file.txt
---Text_files (Subfolder) <- Noten como la carpeta 'Text_files' se copia a si misma
--- folder (subfolder)
--- file2.txt
---file.txt
--- folder (subfolder)
--- file2.txt
As it should be:
My documents (Src)
---file.txt
--- Text_files (Subfolder) Dst
---file.txt
--- folder (subfolder)
--- file2.txt
My code:
import os
import shutil
# Copia archivos filtrándolos por extensiones de una carpeta a otra manteniendo la estructura de carpetas.
def copywf1(Src, Dst, extensions):
try:
item_list = os.listdir(Src)
for item in item_list:
source_fn = os.path.join(Src, item)
if os.path.isdir(source_fn):
copywf1(source_fn, os.path.join(Dst, item), extensions)
elif os.path.splitext(item)[1] in extensions:
if not os.path.exists(Dst):
os.makedirs(Dst)
shutil.copyfile(source_fn, os.path.join(Dst, item))
except Exception as error:
print(error)
pass
copywf1(r"SRC", r"DST", [".txt"])