LMGagne
LMGagne

Reputation: 1716

Move a random sample of files from one folder to another

I have several folders of files, and I'm trying to move a sample of randomly selected files from one folder to another. I'm working with the code below, but it's not quite running.

import os, random, shutil

source='folder_path_1'
dest='dest_folder_path'
onlyfiles = [f for f in os.listdir(source) if os.path.isfile(os.path.join(source, f))]
no_of_files = round((len(onlyfiles)/5))

print(no_of_files)

for i in range(no_of_files):
    random_file=random.choice(os.listdir(source))
    source_file="%s\%s"%(source,random_file)
    dest_file=dest
    shutil.move(source_file,dest_file)

This yields multiple errors. First that dest is not defined, and if I then use the full paths the files don't move (no error, just no movement).

Upvotes: 2

Views: 5814

Answers (3)

Olav Ausland
Olav Ausland

Reputation: 98

import os, random, shutil

source ='C:\\Users\\OlavA\\Desktop\\Start'
destination ='C:\\Users\\OlavA\\Desktop\\Destination'

onlyfiles = [f for f in os.listdir(source) if os.path.isfile(os.path.join(source, f))]
no_of_files = round((len(onlyfiles)/5))

def main():
    for i in range(no_of_files):
        files = [filenames for (filenames) in os.listdir(source)]
        random_file = random.choice(files)
        shutil.move(f'{source}\\{random_file}', destination)


if __name__ == '__main__':
    main()

This works, not the best code at all but it works. Just remember to change your start path and destination path with your own, and you need \\ instead of \ in the path since it's a escape character.

Upvotes: 0

Filip Młynarski
Filip Młynarski

Reputation: 3612

It should look something like this. We could use random.sample() to get specific amount of random unique elements of our list (here files)

import os
import random
import shutil

source = 'folder_path_1'
dest = 'dest_folder_path'
files = os.listdir(source)
no_of_files = len(files) // 5

for file_name in random.sample(files, no_of_files):
    shutil.move(os.path.join(source, file_name), dest)

Upvotes: 7

Electroboss
Electroboss

Reputation: 99

I had no idea what shutil is but I got rid of it completely. I instead just used the built-in open() command. Here's my code:

import os, random

delete = True

source='source'
dest='destination'
onlyfiles = [f for f in os.listdir(source) if os.path.isfile(os.path.join(source, f))]
no_of_files = round((len(onlyfiles)/5))

print(no_of_files)

for i in range(no_of_files):
    random_file=random.choice(os.listdir(source))
    source_file="%s/%s"%(source,random_file)
    dest_file=dest+'/'+random_file
    destination_for_file = open(dest_file,'wb+')
    source_for_file = open(source_file,'rb')
    destination_for_file.write(source_for_file.read())
    destination_for_file.close()
    #optional (to delete the source)
    if delete:
        os.remove(source_file)

Note: if you don't want to delete the source, change delete = True to delete = False

I tested his on my linux machine. If you run Windows (in which case, consider using linux in some way [I dual-boot]), try to change the '/' to '\\'

Upvotes: 0

Related Questions