Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying random files from a file tree

I have the same problem as here but now I'm trying to do the same with python because it's more suited to the task.

I've started with this:

import os
import shutil
import random
import glob


root_dir = '/home/leonardo/Desktop/python_script/rfe'
output_dir = '/home/leonardo/Desktop/python_script/output_folder'
ref = 200

folders_root_dir = os.listdir(root_dir)
print folders_root_dir

count = len(folders_root_dir)
print  count

for i in xrange(count):
    folder_inside = root_dir + '/' + folders_root_dir[i]
    print folder_inside
    number_files_folder_inside = len(os.listdir(folder_inside))
    print  number_files_folder_inside

    if number_files_folder_inside > ref:
        ref_copy = round(0.2*number_files_folder_inside)
        print ref_copy
        # here I have to copy 20% of the files in this folder to the output folder 
    else:
        # here I have to copy all files from the folder to the output_dir

I tried to use os.walk() but I'm new to python and selecting files while the function is working proved to be really tough.

like image 415
LM_O Avatar asked Aug 02 '14 14:08

LM_O


1 Answers

You'll need to import these:

import os
import shutil
import random

You can get all the files in a directory like this:

files = [file for file in os.listdir(dir) if os.path.isfile(os.path.join(dir, file))]

Then use a conditional:

if len(files) < 200:
    for file in files:
        shutil.copyfile(os.path.join(dir, file), dst)
else:
    # Amount of random files you'd like to select
    random_amount = 1000
    for x in xrange(random_amount):
        if len(files) == 0:
            break
        else:
            file = random.choice(files)
            shutil.copyfile(os.path.join(dir, file), outputdir)
like image 53
Thomas Hobohm Avatar answered Oct 10 '22 00:10

Thomas Hobohm