Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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)
  • 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>

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
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...