Back
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?
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>
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
31k questions
32.8k answers
501 comments
693 users