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.
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>
file_path = r"<path to your file>"
if os.stat(file_path).st_size > 0:
<send an email to somebody>
<continue to other things>
Certification in Digital Marketing |
Certification in Big Data Analytics |
Certification in Cloud & Devops |
Certification in Full Stack Web Development
Big Data and Data Science Master’s Course |
Data Science Architect Master’s Program |
Cloud and DevOps Architect Master’s Course |
Artificial Intelligence Engineer Master’s Course |
Microsoft Azure Certification Master’s Training
Data Science Course Online |
Big Data Hadoop Certification Training |
Machine Learning Course Online |
Artificial Intelligence Course and Training |
Salesforce Certification Training: Administrator and App Builder |
Tableau Training and Certification Course |
Python Data Science Course & Training |
Ethical Hacking Course |
Business Analyst Course |
Courses in Cyber Security
© COPYRIGHT 2011-2021 INTELLIPAAT.COM. ALL RIGHTS RESERVED.
Download Salary Trends Now !
Learn how professionals like you got up to 100% Salary Hike.