-
Notifications
You must be signed in to change notification settings - Fork 1
/
randomfile.py
44 lines (39 loc) · 1.05 KB
/
randomfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import math
import random
import sys
from voussoirkit import bytestring
CHUNK_SIZE = 512 * (2 ** 10)
def listget(li, index, fallback=None):
try:
return li[index]
except IndexError:
return fallback
def rid(length=8):
bits = length * 4
bits = random.getrandbits(bits)
identifier = '{:02x}'.format(bits).rjust(length, '0')
return identifier
def make_randomfile(length, filename=None):
if filename is None:
filename = rid(8) + '.txt'
chunks = math.ceil(length / CHUNK_SIZE)
written = 0
f = open(filename, 'w')
for x in range(chunks):
b = min(CHUNK_SIZE, length-written)
f.write(rid(b))
written += b
f.close()
print('Created %s' % filename)
bytes = listget(sys.argv, 1, None)
if bytes is None:
bytes = 2 ** 10
else:
bytes = bytestring.parsebytes(bytes)
filecount = 1
filename = listget(sys.argv, 2, None)
if filename is not None and filename.isdigit():
filecount = int(filename)
filename = None
for x in range(filecount):
make_randomfile(bytes, filename)