'Random selection of files from one folder and recursively copy them into subfolders
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.
Solution 1:[1]
shutil.copyfile fails if the second argument is the directory in
which you want to copy the file. So you have to append the
destination file name to it.
Also, since you want to copy files only in the leaf directories of
Assign_Folder, you have to ignore directories generated by os.walk
that do have sub-directories. You can do that by checking the second
element of tuples generated by os.walk (see Printing final (leaf?)
nodes in directory listing
Python).
With these two patches the code becomes:
import os
import random
import shutil
data_path = 'Data'
root_folder = 'Assign_Folder'
for folder, subs, files in os.walk(root_folder):
if not subs: # ignore the directory if it does have sub-directories
# select 5 random files
filenames = random.sample(os.listdir(data_path), 5)
for fname in filenames:
srcpath = os.path.join(data_path, fname)
dstpath = os.path.join(folder, fname)
shutil.copyfile(srcpath, dstpath)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | qouify |
