Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (33.1k points)

I am writing a Python script in Windows. I want to do something based on the file size. For example, if the size is greater than 0, I will send an email to somebody, otherwise, continue to other things.

How do I check the file size?

1 Answer

0 votes
by (106k points)
edited by
  • There are many ways to check file size in Python some are as follows:-

    • By using os.path.getsize() will return you the file size which will be in bytes. It raises os.error if the file does not exist or is inaccessible.


  • You can also use the os.stat method and its member object st_size. It also returns output in bytes. And this code has full answer to your question.

import os 

file_path = r"<path to your file>" 

if os.stat(file_path).st_size > 0:

  <send an email to somebody> 


  <continue to other things>

Be a Python Expert. Enroll in Python Programming Course by Intellipaat 

Related questions

0 votes
1 answer
asked Oct 14, 2019 in Python by Sammy (47.6k points)
+1 vote
1 answer
asked Jul 9, 2019 in Python by selena (1.6k points)
0 votes
1 answer
asked Aug 27, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
+3 votes
2 answers

Browse Categories