How to check file size in python?
I am writing a Python script in Windows. I want to do something based on the file size. For example, if the size is greater than 0, I will send an email to somebody, otherwise continue to other things.
How do I check the file size?
os.stat, and use the
st_size member of the resulting object:
>>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo (33188, 422511L, 769L, 1, 1032, 100, 926L, 1105022698,1105022732, 1105022732) >>> statinfo.st_size 926L
Output is in bytes.
How to Get File Size in Python, File Size in Python. The python os module has stat() function where we can pass the file name as argument. This function returns a tuple structure that contains the file information. We can then get its st_size property to get the file size in bytes. We can get file size in Python using the os module. File Size in Python. The python os module has stat() function where we can pass the file name as argument. This function returns a tuple structure that contains the file information. We can then get its st_size property to get the file size in bytes.
>>> import os >>> b = os.path.getsize("/path/isa_005.mp3") >>> b 2071611L
The output is in bytes.
How to get file size in Python?, How to get a file's size in Python from the full path of a file? You can use this piece of Python code: os.path.getsize Using pathlib (added in Python 3.4 and 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.
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
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.
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)
How to check file size in python?, There are many ways to check file size in Python some are as follows:- By using os.path.getsize() will return you the file size which will be in There are many ways to check file size in Python some are as follows:-By using os.path.getsize() will return you the file size which will be in bytes. It raises os.error if the file does not exist or is inaccessible. os.path.getsize(file_path) You can also use the os.stat method and its member object st_size. It also returns output in bytes. And this code has full answer to your question.
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)
How to check file size in python / 3 ways to find out size of , Python's os.path module provides a getsize function which takes a file path as argument and returns the size of file in bytes. Example, How to get file size in Python? tagged C, file, Howto, Linux, open, Programming, python, Tutorial.
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.
Python File I/O: Get the file size of a plain file, Write a Python program to get the file size of a plain file. return statinfo.st_size print("File size in bytes of a plain file: ",file_size("test.txt")). Python: Three ways to check if a file is empty; Python : How to get Last Access & Creation date time of a file; Python : Get Last Modification date & time of a file. | os.stat() | os.path.getmtime() How to check if a file or directory or link exists in Python ? Python : How to remove a file if exists and handle errors | os.remove() | os.ulink()
Python: Get file size in KB, MB or GB – human-readable format , Therefore, always check that file exist or not before calling this function. Let's use this function to get the size of a file in bytes,. @696, Python will let you have NULL bytes it strings, but it doesn't make sense to pass those into getsize because the filename can't have NULL bytes in it – John La Rooy Jul 6 '11 at 5:45. I ran both with %timeit on all the files in a given directory and found os.stat to be marginally faster (~6%).
Python, getsize() method in Python is used to check the size of specified path. It returns the size of specified path in bytes. The method raise OSError if the file does not How to check file size in Python. Many times while doing file processing python, we need to know file size in bytes/KBs/MBs. You can get file size using multiple methods, following are two simple methods to get file size using os module. I have run this file for two inputs,one is image and another is empty file. Here is the output of the program.
How to check file size in Python, Many times while doing file processing python, we need to know file size in bytes/KBs/MBs. You can get file size using multiple methods, If you are using Python3 with pathlib you can access os.stat() information using the Path.stat() method, which has the attribute st_size(file size in bytes): >>> from pathlib import Path >>> mypath = Path("path/to/my/file") >>> mypath.stat().st_size == 0 # True if empty