A
A
Aoqqa2019-06-17 10:00:49
linux
Aoqqa, 2019-06-17 10:00:49

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

1 answer(s)
J
Joss Nix, 2019-06-17
@Jossnix

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])

You save in a file with the .py extension. You change pathA and pathB to the desired folders. You start, and everything should turn out))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question