Created
November 25, 2020 19:10
-
-
Save jeremy-rifkin/9b4adb11cecfbd1b1d3abe0f785dab1b to your computer and use it in GitHub Desktop.
Quick utility to pick n random files from a directory
This file contains 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
#!/usr/bin/python | |
import os | |
import shutil | |
import sys | |
import random | |
def pick_random(src, dest, N): | |
all_files = [] | |
for path, dirs, files in os.walk(src): | |
for f in files: | |
full_path = os.path.join(path, f) | |
all_files.append(full_path) | |
random.shuffle(all_files) | |
for i in range(min(len(all_files), N)): | |
shutil.copy(all_files[i], dest) | |
if __name__ == "__main__": | |
if len(sys.argv) != 4: | |
print("Arguments: <src> <dest> <n>") | |
_, src, dest, n = sys.argv | |
if not os.path.exists(dest): | |
os.mkdir(dest) | |
pick_random(src, dest, int(n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment