How to check if a file is empty avoid reading the whole file in a submit form?
<p>I am using django, and in a web submit form, I want to check if a uploading file is empty, but avoid uploading or reading the whole file to get the file size, because it might be huge and take time to calculate.</p>

<p>You are probably looking for <a href="https://docs.python.org/2/library/os.html#os.stat" rel="nofollow">os.stat</a></p>

<p>os.stat(path)</p>

<blockquote>
  <p>Perform the equivalent of a stat() system call on the given path.
  (This function follows symlinks; to stat a symlink use lstat().)</p>
  
  <p>The return value is an object whose attributes correspond to the
  members of the stat structure, namely:</p>
  
  <p>st_mode - protection bits,<br>
   st_ino - inode number,<br>
  st_dev - device,<br>
  st_nlink - number of hard links,<br>
  st_uid - user id of owner,<br>
   st_gid -
  group id of owner,<br>
   <strong>st_size - size of file, in bytes</strong>,</p>
</blockquote>

<p>so you just do</p>

<pre><code>import os
os.stat('/uploaded/file/path')
</code></pre>

python django web