linux - How do I determine an open file's size in Python? -


is a file that I want to make sure it does not exceed 2 GB (as the system should run on Ext Uses 2). What is the best way to keep in mind the size of a file that I will write this file in the middle of the check? In particular, do I need to worry about buffed, unflished changes which have not been written to disk yet?

"post-text" itemprop = "text">

You can start with something like this:

  class TrackedFile (file): Def __init __ (self, file name, mode): self.size = 0 super (TrackedFile, self) .__ init __ (filename, mode) write def (self, s): self.size + = len (s) You can use it like this:  
  & gt; & Gt; & Gt; F = Traced file ('palindrome.txt', 'w') & gt; & Gt; & Gt; F.size 0 & gt; & Gt; & Gt; F.write ('a man planning a canal') & gt; & Gt; & Gt; F.size 21 & gt; & Gt; & Gt; If you are not writing the file from scratch, this implementation does not work, but you can customize your  __ init __   

code> initial Method for handling data For example, you may need to override the listin some other methods.

This works regardless of encoding, because the string is simply the sequence of bytes.

  & gt; & Gt; & Gt; F2 = Tracked file ('palindrome-latin1.txt', 'w') & gt; & Gt; & Gt; F2.write (you 'a man plan a canal'. Encode ('latin1') gt;> gt; f3 = tracked file ('palindrome-utf8.txt', 'w') & gt; | & Gt; & gt; f3 (UTF-8 ')); (Gt; & gt; F2 Size 21 & gt; & gt; & gt; F3 Size 22  
/ div>

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -