title: How to get the size of a list when write to an file by bytes in python?
body: <p>I have a file - which I read it into memory as a list, then split the list based on some rule, say there is list1, list2, .., listn. Now I want to get the size of each list, and this size is the file size when this list write to a file. the following is a code I have, the file name is 'temp', which size is: 744 bytes.</p>
  
  <pre><code>from os import stat
  from sys import getsizeof
  print(stat('temp').st_size) # we get exactly 744 here.
  # Now read file into a list and use getsizeof() function:
  with open('temp', 'r') as f:
  chunks = f.readlines()
  print(getsizeof(chunks)) # here i get 240, which is quite different than 744.
  </code></pre>
  
  <p>since I can't use getsizeof() to directly get the file size (on disk), so once i get the split list, i have to write this list to a tmp file:</p>
  
  <pre><code>open('tmp','w').write("".join(list1))
  print(stat('tmp','w').st_size) # Here is the value I want.
  os.remove('tmp')
  </code></pre>
  
  <p>this solution is very slow and require a lot of write/read to disk. Is there any better way to do? thanks a lot!</p>