0

I have a wordlist generator

import itertools 

s=[ [ 'T', 't'],
    ['E', 'e'],
    ['S', 's'],
    ['T', 't']
]

out = (itertools.product(*s))
for index, entry in enumerate(out): 
    result = ''.join(entry) 
    print(result)
    textfile = open("wordlist.txt","a")
    textfile.write(result)
    textfile.write("\n")
    textfile.close()

How can I check and print the size of the textfile (in GB or KB) that will be generated before saving the file? For example:

python test.py
You will need 20GB to generate wordlist.txt
Louise Fallon
  • 2,705
  • 2
  • 9
  • 14
  • Does this answer your question? [Python : Get size of string in bytes](https://stackoverflow.com/questions/30686701/python-get-size-of-string-in-bytes) – mkrieger1 Feb 02 '20 at 20:57

3 Answers3

0

IIUC, perhaps you can check this:

import numpy as np
nw = len(s)     # number of word characters
nc = np.prod([len(sub) for sub in s])    # number of combinations

size_on_disk = (nw + 1) * nc     # (nw + 2) * nc in case of Windows as of CRLF line endings

This should give you the filesize in Bytes which you can express in kB, MB or whatever easily by dividing by 1024**n.

SpghttCd
  • 10,510
  • 2
  • 20
  • 25
0

You can take the total length of all words in s, then multiple it by the total number of "cells" as it were (num rows * num columns), and take into account the length of your system's newline separator, eg:

import os

# we assume s isn't sparse and each row has the same amount of columns as the first
multiplier = len(s) * len(s[0])
size = (sum(sum(len(el) for el in els) for els in s) + len(os.linesep)) * multiplier
print(f'required space is {size:,} bytes')
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
-1

You can check the file size using this function:

import os

def get_file_size(file_name, size_type = SIZE_UNIT.BYTES ):
    """ Get file in size in given unit like KB, MB or GB"""
    size = os.path.getsize(file_name)
    return convert_unit(size, size_type)


size = get_file_size("wordlist.txt", SIZE_UNIT.GB)
print('Size of file is : ', size ,  'GB')
hamma
  • 19
  • 4