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"])
I tried your code as well as user @Sr1871 with the only variant that we use the
copyfile
library methodshutil
and the result was the following:
I don't see anything wrong with your code.
I just tried your code and what you say happens to me. This is because when you first copy the files, when the for loop goes through the Dst folder and since it is inside Src, the script parses it and thinks they are new files. I don't know how to help you, but thanks to your question I just realized this problem in several of my automation scripts. I hope you manage to solve it and please, post the answer when you have it.
That if only worked at one level because it checked that the folders are the same.
that is, in the following structure
I created the subfolder looking
Dst
like thisWhen it got to the subfolder, the source was
Src/Dst
and the destination wasSrc/Dst
, so when it got to the if then it stoppedThe problem was if the destination was that if it was in a sublevel, since they were not equal it was created, but inside the folder, since they were already equal, it stopped, that is why the cycle was not infinite and that is why between more sublevels it put it nested more times.
This could be a solution