#!/usr/bin/env python3
from __future__ import print_function
from argparse import ArgumentParser
from math import log10
import os
import string
import random
LINE_LENGTH = 80
def create_files(number, size, base, prefix, verbose):
width = int(log10(number)) + 1
fileSize = translate(size)
if verbose:
print("Creating {number} files of size {size}".format(number=number, size=fileSize))
for i in range(number):
filename = os.path.join(base, prefix + "{value:0{width}}".format(value=i, width=width))
if verbose:
print("Creating {} ...".format(filename))
with open(filename, 'w') as f:
lines = int(fileSize / LINE_LENGTH)
for l in range(lines):
f.write(generator(LINE_LENGTH - 1 ) + "\n")
rest = fileSize % LINE_LENGTH
if rest:
f.write(generator(rest - 1) + "\n")
def translate(size):
multi = { 'B': 1, 'K' : 1024, 'M' : 1024 * 1024, 'G' : 1024 * 1024 * 1024 }
if size[-1] in multi:
return int(size[0:-1]) * multi[size[-1]]
else:
return int(size)
def generator(size):
s = string.ascii_letters + string.digits
return "".join((random.choice(s) for x in range(size)))
if __name__ == "__main__":
parser = ArgumentParser(
description="CreateFiles",
epilog="Copyright (C) 2014 Sven Erik Knop, Perforce Software Ltd"
)
parser.add_argument("-n","--number", type=int, default=10, help="Number of files to create (default 10)")
parser.add_argument("-s","--size", default="10", help="File size, [B],K,M,G allowed, default 10")
parser.add_argument("-p", "--prefix", default="File_", help="File prefix, default 'File_'")
parser.add_argument("-b", "--base", default='.', help="Basedir, default it .'")
parser.add_argument("-v", "--verbose", action="store_true", help="Print out progress")
options = parser.parse_args()
create_files(options.number, options.size, options.base, options.prefix, options.verbose)