I need to know the size of a file even before it is created

0

I have a college job and it's hard to solve ... it's kind of the wordlists that hackers use, but it's for teaching.

QT: Generate a file with all possible groupings using the concept of simple arrangements in which we have n elements arranged p to p, with n> = p. Before generating the file, report an output saying how many arrangements and size the file has.

Ex:

  

x Arrays have been generated and the file size is y (bytes, MB, GB, or   ALSO). Do you want to continue, [S] im or [No]?

I do not even know how to start, because how do I calculate the total bytes without even having created the file. Complicated. The way I thought it was to compute the total of arrangements that would give "so many" lines and in each line would have 7 bytes, if each line is 7 bytes, multiply by the total of possible arrangements.

Formula:

  

A (n! / (n-p)!)

Ex:

  

A (22! / (22-7)!) = A (22! / (15)!) = 859.541.760 million words arranged 7 to 7   of 22 elements

Now I get this result and multiply by 7 that would give the total bytes and I would do the conversion to the other sizes. Now why did I multiply by 7? Because each character is 1 byte, taking the characters with accents and the arrangement of all lines are 7 characters long.

The same problem is this, calculate the file size.

    
asked by anonymous 17.04.2016 / 20:31

1 answer

1

To generate a text file you need a string.

texto = 'eu faço\nperguntas\nno SO-pt'

You can generate the binary representation of the string with an encoding.

btexto = texto.encode('utf8')

Once you have the binary string, use len to count the bytes.

n_bytes = len(btexto)

When saving the file, do not forget to use binary mode.

with open('C:\teste.txt', 'wb') as f:
    f.write(btexto)
    
26.10.2018 / 14:24