- How to check file size in python in 3 ways
- Python Check File Size
- os.path.getsize() Method to Check File Size
- Example To Get File Size
- Get File Size in KB, MB, or GB
- os.stat() Method to Check File Size
- Pathlib Module to Get File Size
- Get File Size of a File Object
- Sumary
- About Vishal
- Related Tutorial Topics:
- Python Exercises and Quizzes
- How do I determine an open file's size in Python?
- 7 Answers 7
How to check file size in python in 3 ways
There are different methods to get the size of a file in python and this article explains 3 of those methods with examples.
Finding out a file size becomes important when you want to monitor a particular file for a size limit or when you want to order files in a directory based on their size.
Method 1 : Using os.path module
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,
import os # get the size of file size = os.path.getsize('f:/file.txt') print('Size of file is', size, 'bytes')
Method 2 : Using stat function
Python’s os module provides a stat function which takes a path as argument. This path may be a string or a path object and returns a structure containing statistical details about the path supplied. This structure has many properties out of which, st_size property contains the size of the file(in bytes) present at the supplied path.
Example,
import os # get file stats stats = os.stat('f:/file.txt') print('Size of file is', stats.st_size, 'bytes')
If you print the value returned by os.stats function, then you will get something like this.
os.stat_result(st_mode=33206, st_ino=562949953421562, st_dev=101961010, st_nlink=1, st_uid=0, st_gid=0, st_size=760, st_atime=1554137744, st_mtime=1554137744, st_ctime=1503555351)
Notice that it has an st_size property.
Method 3: Using file object
Open the file whose size needs to be determined using open function and assign the returned object to a variable. The returned object acts as a handle to the file and can be used for various file handling operations.
- Initially when the file is opened, the file cursor points to the beginning of the file. File object has a seek method which is used to set the cursor to a desired location.
It accepts 2 arguments, first is the start location and second is the end location at which the cursor will be placed.
If the first argument is not supplied, then it defaults to 0, meaning the beginning of file. - Set the cursor to the end of the file using seek method supplying it 0 as the start value and os.SEEK_END as the end value where SEEK_END is a constant is os module and marks the end of file.
- File object has a tell method which returns the current position of the cursor. This is the number of bytes that the cursor has moved.
- Thus, when you position the file cursor to the end of the file, tell method will actually return the number of bytes in the file which is also the size of the file.
# open file for reading f = open('f:/file.txt') # move file cursor to end f.seek(0, os.SEEK_END) # get the current cursor position print('Size of file is', f.tell(), 'bytes')
Size of file is 760 bytes
Hope this post will help you in calculating file size using python.
Python Check File Size
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 before performing the file copying operation.
In this article, We will use the following three methods of an OS and pathlib module to get file size.
os.path module:
- os.path.getsize(‘file_path’) : Return the file size in bytes.
- os.stat(file).st_size : Return the file size in bytes
Pathlib module:
os.path.getsize() Method to Check File Size
For example, you want to read a file to analyze the sales data to prepare a monthly report, but before performing this operation we want to check whether the file contains any data.
The os.path module has some valuable functions on pathnames. Here we will see how to use the os.path module to check the file size.
- Important the os.path module This module helps us to work with file paths and directories in Python. Using this module, we can access and manipulate paths
- Construct File Path A file path defines the location of a file or folder in the computer system. There are two ways to specify a file path.
Absolute path: which always begins with the root folder. The absolute path includes the complete directory list required to locate the file. For example, /user/Pynative/data/sales.txt is an absolute path to discover the sales.txt. All of the information needed to find the file is contained in the path string.
Relative path: which is relative to the program’s current working directory.
Example To Get File Size
import os.path # file to check file_path = r'E:/demos/account/sales.txt' sz = os.path.getsize(file_path) print(f'The size is', sz, 'bytes')
E:/demos/account/sales.txt size is 10560 bytes
Get File Size in KB, MB, or GB
Use the following example to convert the file size in KB, MB, or GB.
import os.path # calculate file size in KB, MB, GB def convert_bytes(size): """ Convert bytes to KB, or MB or GB""" for x in ['bytes', 'KB', 'MB', 'GB', 'TB']: if size < 1024.0: return "%3.1f %s" % (size, x) size /= 1024.0 f_size = os.path.getsize(r'E:/demos/account/sales.txt') x = convert_bytes(f_size) print('file size is', x)
os.stat() Method to Check File Size
The os.stat() method returns the statistics of a file such as metadata of a file, creation or modification date, file size, etc.
- First, import the os module
- Next, use the os.stat('file_path') method to get the file statistics.
- At the end, use the st_size attribute to get the file size.
Note: The os.path.getsize() function internally uses the os.stat('path').st_size .
import os # get file statistics stat = os.stat(r'E:/demos/account/sales.txt') # get file size f_size = stat.st_size print('file size is', f_size, 'bytes')
Pathlib Module to Get File Size
From Python 3.4 onwards, we can use the pathlib module, which provides a wrapper for most OS functions.
- Import pathlib module: Pathlib module offers classes and methods to handle filesystem paths and get data related to files for different operating systems.
- Next, Use the pathlib.Path('path').stat().st_size attribute to get the file size in bytes
import pathlib # calculate file size in KB, MB, GB def convert_bytes(size): """ Convert bytes to KB, or MB or GB""" for x in ['bytes', 'KB', 'MB', 'GB', 'TB']: if size < 1024.0: return "%3.1f %s" % (size, x) size /= 1024.0 path = pathlib.Path(r'E:/demos/account/sales.txt') f_size = path.stat().st_size print('File size in bytes', f_size) # you can skip this if you don't want file size in KB or MB x = convert_bytes(f_size) print('file size is', x)
Get File Size of a File Object
Whenever we use file methods such as read() or a write(), we get a file object in return that represents a file.
Also, sometimes we receive a file object as an argument to a function, and we wanted to find a size of a file this file object is representing.
All the above solutions work for a file present on a disk, but if you want to find file size for file-like objects, use the below solution.
We will use the seek() function to move the file pointer to calculate the file size. Let’s see the steps.
- Use the open() function to open a file in reading mode. When we open a file, the cursor always points to the start of the file.
- Use the file seek() method to move the file pointer at the end of the file.
- Next, use the file tell() method to get the file size in bytes. The tell() method returns the current cursor location, equivalent to the number of bytes the cursor has moved, which is nothing but a file size in bytes.
# fp is a file object. # read file fp = open(r'E:/demos/account/sales.txt', 'r') old_file_position = fp.tell() # Moving the file handle to the end of the file fp.seek(0, 2) # calculates the bytes size = fp.tell() print('file size is', size, 'bytes') fp.seek(old_file_position, 0)
Sumary
In this article, We used the following three methods of an OS and pathlib module to get file size.
os.path module:
- os.path.getsize('file_path') : Return the file size in bytes.
- os.stat(file).st_size : Return the file size in bytes
Pathlib module:
Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.
About Vishal
I’m Vishal Hule, Founder of PYnative.com. I am a Python developer, and I love to write articles to help students, developers, and learners. Follow me on Twitter
Related Tutorial Topics:
Python Exercises and Quizzes
Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.
- 15+ Topic-specific Exercises and Quizzes
- Each Exercise contains 10 questions
- Each Quiz contains 12-15 MCQ
How do I determine an open file's size in Python?
There's a file that I would like to make sure does not grow larger than 2 GB (as it must run on a system that uses ext 2). What's a good way to check a file's size bearing in mind that I will be writing to this file in between checks? In particular, do I need to worry about buffered, unflushed changes that haven't been written to disk yet?
Is there a reason you can't just keep track of the file size yourself - that is, see what the size is when you open it and increment a counter when you write? Not particularly elegant, but it should work.
The maximum file size limit under ext2 is 16GiB -- 64TiB depending on the block size. See en.wikipedia.org/wiki/Ext2. This doesn't answer your question, but just thought this might be helpful.
Jason, what would happen if you let the file grow too large? Generally in Python, try not to "look before you leap". let exceptions occur, and handle them then. Usually faster and cleaner. What would you do if your counter said the file was about to become too large? Can you do the same after catching an exception when it does get too large? Some extra detail might help in your question.
7 Answers 7
Perhaps not what you want, but I'll suggest it anyway.
import os a = os.path.getsize("C:/TestFolder/Input/1.avi")
Alternatively for an opened file you can use the fstat function, which can be used on an opened file. It takes an integer file handle, not a file object, so you have to use the fileno method on the file object:
a = open("C:/TestFolder/Input/1.avi") b = os.fstat(a.fileno()).st_size
os.fstat(file_obj.fileno()).st_size should do the trick. I think that it will return the bytes written. You can always do a flush before hand if you are concerned about buffering.
Though this is an old question, I think that Isak has the simplest solution. Here's how to do it in Python:
# Assuming f is an open file >>> pos = f.tell() # Save the current position >>> f.seek(0, 2) # Seek to the end of the file >>> length = f.tell() # The current position is the length >>> f.seek(pos) # Return to the saved position >>> print length 1024
I think that in the first line (save current position), you should use f.tell(), not the seek(), which would cause an exception since seek() needs at least 1 argument.
This will calculate the file size correctly, but won't restore the position correctly due to known issues with tell in append mode.
@personal_cloud I thought that would not be an issue as long as you did not write between the tell and seek , but I may be wrong. I didn't have an issue in my tests, but it looks like those issues vary by platform. Thanks for pointing that out.
You could start with something like this:
class TrackedFile(file): def __init__(self, filename, mode): self.size = 0 super(TrackedFile, self).__init__(filename, mode) def write(self, s): self.size += len(s) super(TrackedFile, self).write(s)
Then you could use it like this:
>>> f = TrackedFile('palindrome.txt', 'w') >>> f.size 0 >>> f.write('A man a plan a canal ') >>> f.size 21 >>> f.write('Panama') 27
Obviously, this implementation doesn't work if you aren't writing the file from scratch, but you could adapt your __init__ method to handle initial data. You might also need to override some other methods: writelines , for instance.
This works regardless of encoding, as strings are just sequences of bytes.
>>> f2 = TrackedFile('palindrome-latin1.txt', 'w') >>> f2.write(u'A man a plan a canál '.encode('latin1') >>> f3 = TrackedFile('palindrome-utf8.txt', 'w') >>> f3.write(u'A man a plan a canál '.encode('utf-8')) >>> f2.size 21 >>> f3.size 22