Problem
In Windows, I’m building a Python script. I’d like to do something based on the size of the file. If the size is more than 0, for example, I’ll send an email to someone; otherwise, I’ll move on to something else.
How do I check the file size?
Asked by 5YrsLaterDBA
Solution #1
Using os.path.getsize:
>>> import os
>>> b = os.path.getsize("/path/isa_005.mp3")
>>> b
2071611
Bytes are used to represent the output.
Answered by danben
Solution #2
The st size property of the object returned by os.stat is required. It can be obtained by 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.
Answered by Adam Rosenfield
Solution #3
The previous responses are for real files; however, if you need anything for “file-like objects,” try this:
# f is a file-like object.
f.seek(0, os.SEEK_END)
size = f.tell()
In my limited testing, it works for both real files and StringIOs. (This is Python 2.7.3.) Of course, the “file-like object” API isn’t quite a strict interface, but the API documentation indicates that file-like objects should implement seek() and tell() ().
Edit
You can stat() a file even if you don’t have permission to access it, which is another difference between this and os.stat(). Obviously, you won’t be able to use the seek/tell strategy unless you have read permission.
Edit 2
Here’s a paranoid variant, as suggested by Jonathon. (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)
Answered by Mark E. Haase
Solution #4
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
Answered by Rajiv Sharma
Solution #5
Pathlib (added in Python 3.4 or available as a backport on PyPI):
from pathlib import Path
file = Path() / 'doc.txt' # or Path('./doc.txt')
size = file.stat().st_size
This is really just a wrapper for os.stat, but pathlib makes it simple to access other file-related actions.
Answered by Michael Mulich
Post is based on https://stackoverflow.com/questions/2104080/how-can-i-check-file-size-in-python