Answer the question
In order to leave comments, you need to log in
How to choose a random folder, copy all the files from it and move it to another folder?
Good afternoon!
Please help me write a script.
There is a folder A with a number of other folders. It is required to select a random folder from this folder and copy all the files in it, then move the copied files to folder B
Answer the question
In order to leave comments, you need to log in
import os
import random
import shutil
pathA ='e:/Work/tasks/'
pathB = 'e:/Shared_WR/'
folders = []
all_files = os.listdir(pathA)
for item in all_files:
if (os.path.isdir(item)):
folders.append(item)
print (folders)
int_random = random.randint(0, len(folders))
print (int_random)
shutil.copytree(folders[int_random], pathB+folders[int_random])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question