I have 500 zip files in a folder called Data from which I need to randomly select 5 files and copy those files to the subfolders. I have the folder structure as follows:
Root_Folder
|__Data (contains 500 zip files)
|__Assign_Folder
|__One (subfolder)
|__a1 (sub-subfolder)
|__a2 (and so on)
|__Two (subfolder)
|__a1 (sub-subfolder)
|__a2 (and so on)
|__script.py
The Python script is located in the Root_Folder. I want to randomly select 5 zip files from the Data folder and recursively copy them in each of the a1, a2, a3, ... subfolders inside the One, Two, ... parent folders. So, in the end I want to have 5 zip files each copied into the a1, a2, ... folders inside the One, Two, ... parent folders. There are enough subfolders that the files can be easily copied.
I have written the following code:
import os
import random
import shutil
data_path = 'Data'
root_folder = 'Assign_Folder'
for folder, subs, files in os.walk(root_folder):
# select 5 random files
filenames = random.sample(os.listdir(data_path), 5)
for fname in filenames:
srcpath = os.path.join(data_path, fname)
shutil.copyfile(srcpath, folder)
This code is giving me an error IsADirectoryError: [Errno 21] Is a directory: 'Assign_Folder'
Please help me in correcting and completing this task. Thank you.