This post will discuss how to get the size of a file in Python.

1. Using os.stat() function

The standard solution to get a file’s status is using the os.stat() Python function. It returns a stat_result object, which has a st_size attribute containing the file’s size in bytes.

2. Using Path.stat() function

Alternatively with Python 3.4, you can use the Path.stat() function from pathlib module. It is similar to the os.stat() function and returns stat_result object containing information about the specified path.

3. Using os.path.getsize() function

Another good option is to use the os.path.getsize() function to get the size of the specified path in bytes.

4. Using seek() function

Here, the idea is to open the file in read-only mode and set the current position of the file descriptor at the end. This can be done using the seek() function, which returns the current cursor position in bytes, starting from the beginning.

That’s all about getting the size of a file in Python.