Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
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.

os.path.getsize(file_path)

  • 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> 

else: 

  <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)
0 votes
1 answer
+3 votes
2 answers
0 votes
2 answers
asked Aug 23, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...