How do I get the size of a file in Python?
-
27`Path('./doc.txt').stat().st_size` – Boris Verkhovskiy Dec 31 '19 at 18:21
-
1Thanks @Boris for the modern Python (v3.4+) answer :) – mab Mar 15 '20 at 19:01
-
Please also get familiarity with a phenomenon of sparse files – eugene-bright Oct 08 '21 at 11:52
10 Answers
Use os.path.getsize:
>>> import os
>>> os.path.getsize("/path/to/file.mp3")
2071611
The output is in bytes.
- 21,459
- 16
- 82
- 123
- 77,526
- 18
- 119
- 143
-
159Note: the implementation of `os.path.getsize` is simply `return os.stat(filename).st_size` – wim Mar 21 '13 at 11:20
-
1So is there a minute performance loss from using os.path.getsize as opposed to os.stat(file).st_size? – wordsforthewise May 18 '15 at 01:45
-
9
-
2@wordsforthewise this is more of an issue if you also want to get other things about the file (modification time, type of file, e.g.) -- then you might as well get it all from a single system call via `os.stat`. Then the difference could run into a substantial number of microseconds :-) – greggo Dec 21 '19 at 18:24
-
-
6and then divide by 1e+6 to get file size in MB, example: output/1e+6 – Biplob Das Jul 14 '20 at 06:37
-
output/1e+6 is not exactly in MBs, at least in my OS. output/(1<<20) works for me. – Sourav Kannantha B Sep 28 '21 at 14:05
You need the st_size property of the object returned by os.stat. You can get it by either using pathlib (Python 3.4+):
>>> from pathlib import Path
>>> Path('somefile.txt').stat()
os.stat_result(st_mode=33188, st_ino=6419862, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=1564, st_atime=1584299303, st_mtime=1584299400, st_ctime=1584299400)
>>> Path('somefile.txt').stat().st_size
1564
or using os.stat:
>>> import os
>>> os.stat('somefile.txt')
os.stat_result(st_mode=33188, st_ino=6419862, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=1564, st_atime=1584299303, st_mtime=1584299400, st_ctime=1584299400)
>>> os.stat('somefile.txt').st_size
1564
Output is in bytes.
- 10,733
- 7
- 77
- 79
- 375,615
- 96
- 501
- 581
-
2If anything, the value could be passed as multiples of the file system block size (4096 bytes for example). Gladly, it is given as bytes instead. – josch Feb 13 '16 at 08:36
-
1@josch - yes, this is nice, for the "size on disk" you can multiply `stat_result.st_blocks` by the block size, but I'm still searching how to get it programmatically and cross-platform (not via `tune2fs` etc.) – Tomasz Gandor Apr 22 '16 at 20:56
The other answers work for real files, but if you need something that works for "file-like objects", try this:
# f is a file-like object.
f.seek(0, os.SEEK_END)
size = f.tell()
It works for real files and StringIO's, in my limited testing. (Python 2.7.3.) The "file-like object" API isn't really a rigorous interface, of course, but the API documentation suggests that file-like objects should support seek() and tell().
Edit
Another difference between this and os.stat() is that you can stat() a file even if you don't have permission to read it. Obviously the seek/tell approach won't work unless you have read permission.
Edit 2
At Jonathon's suggestion, here's a paranoid version. (The version above leaves the file pointer at the end of the file, so if you were to try to read from the file, you'd get zero bytes back!)
# f is a file-like object.
old_file_position = f.tell()
f.seek(0, os.SEEK_END)
size = f.tell()
f.seek(old_file_position, os.SEEK_SET)
- 913
- 1
- 17
- 33
- 24,819
- 11
- 63
- 70
-
9You don't need to import `os`, instead write `f.seek(0, 2)` to seek 0 bytes from the end. – cdosborn Apr 03 '15 at 03:58
-
2And for the last line, if ```os``` isn't used: ```f.seek(old_file_position, 0)``` – luckydonald Dec 02 '15 at 15:11
-
62If you use integer literals instead of named variables, you are torturing anybody that has to maintain your code. There's no compelling reason not to import `os`. – Mark E. Haase Dec 02 '15 at 16:25
-
Thanks for the solution, I've implemented and it's working fine. Just to confirm, `size` output is in bytes? – Kedar.Aitawdekar May 28 '18 at 05:54
-
3Apparently this is at least a little risky, depending on how Python implements `#seek()`: https://wiki.sei.cmu.edu/confluence/display/c/FIO19-C.+Do+not+use+fseek%28%29+and+ftell%28%29+to+compute+the+size+of+a+regular+file – Translunar Aug 17 '18 at 20:35
import os
def convert_bytes(num):
"""
this function will convert bytes to MB.... GB... etc
"""
for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
if num < 1024.0:
return "%3.1f %s" % (num, x)
num /= 1024.0
def file_size(file_path):
"""
this function will return the file size
"""
if os.path.isfile(file_path):
file_info = os.stat(file_path)
return convert_bytes(file_info.st_size)
# Lets check the file size of MS Paint exe
# or you can use any file path
file_path = r"C:\Windows\System32\mspaint.exe"
print file_size(file_path)
Result:
6.1 MB
- 6,032
- 48
- 52
-
6`this function will convert bytes to MB.... GB... etc` Wrong. This function will convert bytes to MiB, GiB, etc. See [this post](https://superuser.com/a/1077275/174299). – moi Jul 18 '17 at 07:30
-
3Line 10 can be changed to `return f'{num:.1f} {x}'` in Python >= 3.5. – Mattwmaster58 Jun 07 '18 at 23:40
-
thank you Matt M., slight update, line 10 can be changed to `return f'{num}{unit}' if unit == 'bytes' else f'{num:.1f}{unit}' ` in Python >= 3.5 – MZA Aug 03 '20 at 15:05
Using pathlib (added in Python 3.4 or a backport available on PyPI):
from pathlib import Path
file = Path() / 'doc.txt' # or Path('./doc.txt')
size = file.stat().st_size
This is really only an interface around os.stat, but using pathlib provides an easy way to access other file related operations.
- 49,954
- 14
- 144
- 128
- 1,158
- 13
- 20
There is a bitshift trick I use if I want to to convert from bytes to any other unit. If you do a right shift by 10 you basically shift it by an order (multiple).
Example:
5GB are 5368709120 bytes
print (5368709120 >> 10) # 5242880 kilobytes (kB)
print (5368709120 >> 20 ) # 5120 megabytes (MB)
print (5368709120 >> 30 ) # 5 gigabytes (GB)
- 30,030
- 21
- 100
- 124
- 21,126
- 14
- 125
- 152
-
12This doesn't answer the question. The question is about finding the size of a file, not about formatting the result for human consumption. – Will Manley Apr 09 '18 at 12:44
-
2These numbers are wrong and thus confusing. 5GB is 5e9 bytes. Is this supposed to be some sort of human-readable approximation? Where would you even use something like this? – Dre Aug 14 '18 at 00:29
-
11-bit=>2 ... 2-bits=>4 ... 3-bits=>8 ... 4-bits=>16 ... 5-bits=>32 ... 6-bits=>64 ... 7-bits=>128 ... 8-bits=>256 ... 9-bits=>512 ... 10-bits=>1024 ... 1024 bytes is 1kB ... => 20-bits => 1024 * 1024 = 1,048,576bytes, which is 1024kB, and 1MB... => 30-bits => 1024 * 1024 * 1024 = 1,073,741,824 bytes, which is 1,048,576 kB, and 1024MB, and 1GB … You have confused scientific notation and decimal places with the binary/base-2 representation used in computing. 5x9 = 5 x 10^9 = 5,000,000,000 – James 'Fluffy' Burton Sep 12 '18 at 15:33
-
5Guys, he hasn't *confused* anything... he's just given an approximation, which is evident when he says "basically". 2^10 is approx. 10^3. In fact, this approximation is so common that [it has a name](https://en.wikipedia.org/wiki/Mebibyte): *Mebi*, *Gibi*, and *Tebi* are Mega, Giga, and Tera, respectively. Regarding not answering the question, @WillManley , you have a fair point there! ;-p – Mike Williamson Oct 02 '18 at 23:36
-
@WillManley it does not answer the question but it gives the OP **more learnings** probably the person who answer this could edit the question and that this trick. Thanks for this .. I needed this – Ice Bear Jan 21 '21 at 04:40
-
A package such as [`humanize`](https://pypi.org/project/humanize/) will do this via its `naturalsize` function. – Asclepius Feb 24 '21 at 16:29
-
Strictly sticking to the question, the Python code (+ pseudo-code) would be:
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>
- 30,030
- 21
- 100
- 124
- 2,168
- 2
- 19
- 13
we have two options Both include importing os module
1)
import os
os.stat("/path/to/file").st_size
as os.stat() function returns an object which contains so many headers including file created time and last modified time etc.. among them st_size gives the exact size of the file.
File path can be either absolute or relative.
2) In this, we have to provide the exact file path, File path can be either relative or absolute.
import os
os.path.getsize("path of file")
- 157
- 2
- 7
You can use the stat() method from the os module. You can provide it with a path in the form of a string, bytes or even a PathLike object. It works with file descriptors as well.
import os
res = os.stat(filename)
res.st_size # this variable contains the size of the file in bytes
- 1,063
- 2
- 12
- 18
#Get file size , print it , process it...
#Os.stat will provide the file size in (.st_size) property.
#The file size will be shown in bytes.
import os
fsize=os.stat('filepath')
print('size:' + fsize.st_size.__str__())
#check if the file size is less than 10 MB
if fsize.st_size < 10000000:
process it ....
- 1,660
- 1
- 14
- 24