Use os.path.getsize:

>>> import os
>>> os.path.getsize("/path/to/file.mp3")
2071611

The output is in bytes.

Answer from danben on Stack Overflow
Discussions

python - File size is zero - Stack Overflow
I'm trying to write a simple script that looks at various files and manipulates them depending on file size, but I've tried both : ... but both return a result of '0'. I'm running Kubuntu but the file system is NTFS formatted, could this be the problem, or does anyone have any reason why this ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 22, 2017
Loosing my patience and hairs over this - getting size of file returns 0
OK i found the issue. It was related with fdialog.asksaveasfile which calls save_config() method. As it seems it truncates file when you want to overwrite existing one. Thats why it always shows 0. More on reddit.com
๐ŸŒ r/learnpython
11
4
April 11, 2024
python - Is it normal that os.stat("file path").st_size returns 0 during the download process? - Stack Overflow
So I thought maybe inside a loop I can check how the size of the file is being evolved as it is being downloaded and once there is no more change that means the end of the download process. I wrote a few test programs and I debugged via pdb to see how exactly the file size evolves during the download process. This is my final solution: import os def main(): st_size = 0 ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Checking if a file is empty?
Use os.stat(file).st_size == 0 in the for loop to check if it's empty. More on reddit.com
๐ŸŒ r/learnpython
26
53
August 7, 2020
๐ŸŒ
Quora
quora.com โ€บ In-Python-how-would-you-write-this-in-code-if-file-size-is-greater-than-or-equal-to-1-byte-execute-if-file-size-is-0-bytes-do-not-execute
In Python, how would you write this in code: 'if file size is greater than or equal to 1 byte, execute; if file size is 0 bytes, do not execute'? - Quora
Answer (1 of 2): You can use [code ]os.path.getsize()[/code] to get size of a file in bytes. Following is the piece of code you can use. If a file does not exists the [code ]getsize()[/code] module can raise OSError. If you need to handle cases where a file does not exists, you can write exceptio...
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ file handling โ€บ python check file size
Python Check File Size
December 29, 2021 - In this tutorial, youโ€™ll learn how to get file size in Python. Whenever we work with files, sometimes we need to check file size before performing any operation. For example, if you are trying to copy content from one file into another file. In this case, we can check if the file size is greater than 0 ...
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-check-if-file-is-empty
Check if File is Empty in Python
To check if a file is empty or not in Python, you can use the os.path library. Import os library, get the file size using os.path.getsize() function and check if the size is zero or not. Empty file must have a size of zero bytes. The syntax of the boolean expression to check if the file is ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ check-if-a-text-file-empty-in-python
Check If a Text File Empty in Python - GeeksforGeeks
December 11, 2024 - Python ยท # import required libraries ... os.path.getsize(file_path) # if file size is 0, it is empty if file_size == 0: print("File is empty") else: print("File is NOT empty") # if file does not exist, then exception occurs except ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ loosing my patience and hairs over this - getting size of file returns 0
r/learnpython on Reddit: Loosing my patience and hairs over this - getting size of file returns 0
April 11, 2024 -

When i do it directly ...

>>> import os

size = os.path.getsize('Untitled.rewasd') print(size) 30063

So it works. Now my method ...

    def save_config(self,buttons_dict):
    self.buttons_dict = buttons_dict

    self._generate_jsonfile()
    
    #size = os.path.getsize(self.file_path)

    print(self.file_path)
    stat = os.stat(self.file_path)
    size = stat.st_size
    print(size)

        # rest is irrelevant
    file_exists = os.path.getsize(self.file_path) > 0

    f = open(self.file_path, "a+") # open original file

    if file_exists: # if file exists and is not empty
        self.rewasd_file_bckp_dict = json.load(f)
        f_bckp = open(self.file_path + ".bak", "w") # create backup file
        f_bckp.write(json.dumps(self.rewasd_file_bckp_dict, indent=4)) # write original content to backup file
        f_bckp.close()
    
    f.write(json.dumps(self.rewasd_file_dict, indent=4)) # write new content to backup file
    f.close()

gives me

python3 main.py 

/my/path/to/file/Untitled.rewasd 0

- file size call is above first file opening > https://stackoverflow.com/questions/63091396/os-path-getsize-returns-0

- tried both os.path and os.stat

- all other methods which open file have also .close() for each of them > ful class https://pastebin.com/tyG6Y0yw

Why im still getting this??

Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-get-file-size-in-python
How to get file size in Python? - GeeksforGeeks
July 23, 2025 - So this method actually returns the size of the file in bytes. Example: Python3 ยท # approach 3 # using file object # open file file = open('d:/file.jpg') # get the cursor positioned at end file.seek(0, os.SEEK_END) # get the current position of cursor # this will be equivalent to size of file print("Size of file is :", file.tell(), "bytes") Output: Size of file is : 218 bytes ยท
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 66890005 โ€บ is-it-normal-that-os-statfile-path-st-size-returns-0-during-the-download-pro
python - Is it normal that os.stat("file path").st_size returns 0 during the download process? - Stack Overflow
So I thought maybe inside a loop I can check how the size of the file is being evolved as it is being downloaded and once there is no more change that means the end of the download process. I wrote a few test programs and I debugged via pdb to see how exactly the file size evolves during the download process. This is my final solution: import os def main(): st_size = 0 while (st_size == 0): st_size = os.stat("my_big_file.zip").st_size print("Download terminated and the size is : {}".format(str(st_size))) if __name__ == "__main__": main()
๐ŸŒ
Codingem
codingem.com โ€บ home โ€บ python how to check file size (theory & examples)
Python How to Check File Size (Theory & Examples)
December 12, 2022 - Here you open the file using the open() function and then seek to the end of the file by passing 0 as the first argument and 2 as the second argument to file.seek(). This sets the file pointer to the end of the file.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ file โ€บ python-io-exercise-11.php
Python File I/O: Get the file size of a plain file - w3resource
July 22, 2025 - Write a Python script to get the size of a file and format the size in a human-readable format (e.g., MB, GB).
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-check-if-file-is-empty
How to check if a File is Empty in Python | bobbyhadz
April 10, 2024 - If the os.path.getsize() method returns 0, then the file has a size of 0 bytes and is empty. ... Copied!import os # ๐Ÿ‘‡๏ธ with relative path if os.path.getsize('example.txt') == 0: print('The file is empty') else: print('The file is NOT empty') ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ checking if a file is empty?
r/learnpython on Reddit: Checking if a file is empty?
August 7, 2020 -

I'm trying to determine whether or not a file is empty while iterating through a folder.

def parse_files(results):
    for file in results[0]:
        with open(file) as my_file:
            #if file size is 0, return False

I feel like the answer is so easy but i've been stuck for a bit.

๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-get-file-size-in-python
How to Get File Size in Python with os and pathlib | DigitalOcean
September 8, 2025 - Raw byte counts can be hard to interpret, especially for larger files. To display sizes in a human-readable format, you can use a helper function that divides the size by 1024 repeatedly and appends the correct unit: def format_size(size_bytes, decimals=2): if size_bytes == 0: return "0 ...
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ python check file size
How to Check File Size in Python | Delft Stack
February 2, 2024 - This Pythonโ€™s module os.path has a function getsize) that returns the file size in bytes by taking the file path as the argument. ... This Python os module also provides a stat method to check the file size. It also takes the file path as the argument and returns a structure type object.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-check-file-size-in-python
How to check file size in Python? - GeeksforGeeks
December 17, 2020 - os.path.getsize() function only works with os Library, with the help of importing this library we can use this to get the size of any type of file and the output of this function will be the size of the file in bytes.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-check-the-file-size
How to check the file size?
6 days ago - The size of a file is measured ... represented by a single value: either 0 or 1. Bytes are used to measure the amount of data in a file and the bytes are denoted by 8 bits. There are several ways to check the file size in python....
๐ŸŒ
Studytonight
studytonight.com โ€บ python-howtos โ€บ how-to-check-whether-a-file-is-empty-or-not
How to check whether a File is Empty or not - Studytonight
February 9, 2021 - The os module provides another function os.path.getsize() to check whether a file is empty or not. It takes the file path as an argument. It will check for the file size. If the file size is 0, it will print the file as Empty.
๐ŸŒ
Studytonight
studytonight.com โ€บ python-howtos โ€บ how-to-check-file-size-in-python
How to Check file size in Python - Studytonight
January 29, 2021 - This will store the size into the size variable starting from 0 to end. The difference between seek/tell and os.stat() is that you can stat() a file even if you don't have permission to read it. So, the seek/tell approach won't work unless you have read permission. In this article, we learned how to check the file size by using several built-in functions such as seek(), tell(), st_size(), and os.path.getsize().