Categories
Python Answers

How to check file size in Python?

Spread the love

Sometimes, we want to check file size in Python.

In this article, we’ll look at how to check file size in Python.

How to check file size in Python?

To check file size in Python, we can use the os.path.getsize method.

For instance, we write:

import os

b = os.path.getsize("file.txt")
print(b)

We call os.path.getsize with the file path of the file to get the size of.

Therefore b is a number in bytes.

Conclusion

To check file size in Python, we can use the os.path.getsize method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *