Skip to content

Instantly share code, notes, and snippets.

@Ovyerus
Last active May 1, 2017 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ovyerus/c864402de7b8072247d92a1871e49a6d to your computer and use it in GitHub Desktop.
Save Ovyerus/c864402de7b8072247d92a1871e49a6d to your computer and use it in GitHub Desktop.
Script to generate a directory with a random name and with random files with random extensions and random content.
import os, string, random, re, argparse, sys, shutil
parser = argparse.ArgumentParser(description='Generate a random directory with randomly generated files within.')
parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.4', help='Display the version of the program.')
parser.add_argument('-a', '--amount', default=random.randrange(50, 200), type=int, help='Set the amount of files to generate.')
parser.add_argument('-r', '--remove', action='store_true', help='Remove all directories that follow the format created by this program.')
args = parser.parse_args()
amt = args.amount;
rand_gen_string = string.ascii_uppercase + string.ascii_lowercase + string.digits + '-_'
rand_dir = 'tfdc_' + ''.join(random.choice(rand_gen_string) for _ in range(8))
exts = ('.txt', '.py', '.md', '.js', '.html', '.conf', '.ini', '.json', '.cs', '.c', '.cpp', '.prolog')
def yes(confirm):
return (confirm == '') or (re.match('^y(es)?$', confirm) is not None)
def no(confirm):
return re.match('^no?$', confirm) is not None
def matches_tmp_folder(folder_name):
return re.match('tfdc_[a-zA-Z0-9-_]{8}', folder_name) is not None
def create_files():
if not os.path.exists('./' + rand_dir):
os.makedirs('./' + rand_dir)
print('Created directory "./{}\n'.format(rand_dir))
for i in range(amt):
f_name = ''.join(random.choice(rand_gen_string) for _ in range(8))
f_name += random.choice(exts)
f = open(rand_dir + '/' + f_name, 'a')
f.write(''.join(random.choice(rand_gen_string) for _ in range(random.randrange(50, 1000))))
f.close()
print('Created file "./{}/{}"'.format(rand_dir, f_name))
print('\nFinished creating {} files in directory "./{}"'.format(amt, rand_dir))
def remove_files():
folders = [x for x in next(os.walk('.'))[1] if matches_tmp_folder(x)]
n = 0
if len(folders) == 0:
print('No folders in the current directory appear to be generated by the program.')
print('Exiting...')
else:
for folder in folders:
shutil.rmtree(folder)
print('Removed directory "{}"'.format(folder))
n += 1
print('\nFinished removing {} directories.'.format(n))
if args.remove:
print('This will remove all the directories in the current directory that appear to match the format generated by this program.')
sys.stdout.write('Do you wish to continue [Y/n]? ')
rename_confirm = input().lower()
print('')
if yes(rename_confirm):
remove_files()
elif no(rename_confirm):
print('Exiting...')
else:
create_files()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment