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

Asked

Viewed 818 times

0

I’m on a college job and it’s hard to solve... it’s like those wordlists that hackers use, but it’s for didactic study.

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

Ex:

Were generated x arrangements and the file size is y (bytes, MB, GB or TB). Continue, [S]im or [N]ão?

I don’t even know how to start, because how do I calculate the total bytes without even having created the file. Complicated. The way I thought was to calculate the total of arrangements that would give "so many" lines and on each line would have 7 bytes, if each row is 7 bytes, multiplied by the total 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 take this result and multiply by 7 that would give the total bytes and I would convert to the other sizes. Now why did I multiply by 7? Because each character is worth 1 byte, apart from the accented characters and the arrangement of all lines are with 7 characters.

The same problem is this, calculating the file size.

  • Depending on the structure of your arrangement, it can be the size of your file, if your arrangement is in string, use the len to get the size, when writing to the file the size will be the same

  • In the module sys has the method getsizeof() that allows you to get the size of an object in bytes, I believe this method can help you to get the size of an object that will be the content of a particular file before its creation.

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, don’t forget to use binary mode.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.