Created
October 9, 2012 19:31
-
-
Save ColtonPhillips/3860909 to your computer and use it in GitHub Desktop.
Retrieves a variable amount of random files of a given type and copys them to an output directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
import string | |
import random | |
import shutil | |
# Usage: "python Randomizer.py root_dir out_dir number_to_get [file_extensions]" | |
# file_extensions e.g. -> ".mp3 .wav .ogg .flac" | |
if __name__ == '__main__': | |
# Directory to get stuff, number of stuff to get | |
root = sys.argv[1] | |
out = sys.argv[2] | |
num = sys.argv[3] | |
# List of exceptable file extensions | |
file_types = list() | |
for arg in list(sys.argv[4:]): | |
file_types.append(arg) | |
#clear output dir | |
for out_root, out_subFolders, out_files in os.walk(out): | |
for file in out_files: | |
os.remove(os.path.join(out,file)) | |
#create candidates for random list | |
candidate_list = list() | |
for in_root, in_subFolders, in_files in os.walk(root): | |
for file in in_files: | |
ext_index = string.rfind(file,".") | |
if ext_index != -1: #no extension | |
for type in file_types: | |
if file[ext_index + 1:] == type: | |
candidate_list.append([os.path.join(in_root,file), file]) | |
#shuffle the mo' fuckas | |
random.shuffle(candidate_list) | |
#slice em to the number we gave | |
final_list = candidate_list[:int(num)] | |
#copy the files | |
for list in final_list: | |
shutil.copyfile(list[0],os.path.join(out,list[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment