Python Verify File Size
You will learn how to determine the size of a file using Python by following along with this tutorial.
While working with files of any kind, it is occasionally necessary to verify the size of the file before carrying out any activity. Take, as an example, the scenario in which you are attempting to transfer material from one file into another one. Before carrying out the process of copying the file, we may carry out a check to see whether or not the size of the file is more than 0.
In this post, we will determine the size of a file using the following three ways of an operating system and pathlib module. module for os.path
:
-
os.path.getsize('file_path')
: Return the file size in bytes. -
os.stat(file).st_size
: Return the file size in bytes
Pathlib module
:
-
pathlib.Path('path').stat().st_size
: Return the file size in bytes.
Method for Determining the Size of a File: os.path.getsize()
For illustration’s sake, say you want to read a file in order to examine the sales data and generate a monthly report. But, prior to carrying out this operation, we want to determine whether or not the file includes any data at all. The os.path module offers a number of useful methods for working with pathnames. In this section, we will demonstrate how to inspect the file using the os.path module.
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.To maintain uniformity across the operating system,
use the forward-slash
(
/
) to separate the path. It’ll work across Windows, macOS, and Unix-based systems, including Linux. -
Use os.path.getsize() function
Use the
os.path.getsize('file_path')
function to check the file size. Pass the file name or file path to this function as an argument.
This function returns file size in bytes
. It raises
OSError
if the file does not exist or is inaccessible.
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 {file_path} size is', sz, 'bytes')
Run Output
:
E:/demos/account/sales.txt size is 10560 bytes
Obtain File Size in KB, MB, or
GB
- First, get the file size using the getsize() function.
- Next, convert bytes to KB or MB.
To determine the size of the file in KB, MB, or other units, use the following example.
GB.
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')
Run Output
:
file size is 10.3 KB
os.stat() is a Method for Checking the Size of a File
The os.stat() function retrieves the statistics of a file, including the metadata of the file, the date the file was created or modified, the size of the file, and so on.
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() method relies on the os.stat(‘path’) for its internal workings. st size . Example
:
# 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)
Run Output
:
file size is 10560 bytes
Pathlib Module to Determine the Size of a File
Since Python version 3.4, we are able to utilise the pathlib module, which offers a wrapper for the majority of operating systems.
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
Example
:
code@@@6
Run Output
:
file size is 10.3 KB
Find out how big a file or file object is.
After using any of the file methods, such as read() or write(), we will get a file object that acts as a representation of the file in question.
In addition, there are occasions when we are sent a file object as a parameter to a function, and we need to determine the size of the file that this file object is supposed to represent.
If you wish to determine the file size of an object that is similar to a file, use the solution that is shown below rather than any of the options that were presented before.
In order to determine the size of the file, we will be moving the file pointer by using the seek() method. Have a look at 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.
Example
:
code@@@8
Run Output
:
file size is 10560 bytes
Sumary
For determining the size of a file for this article, we employed each of the following three techniques provided by an operating system and pathlib module. module for os.path
:
-
os.path.getsize('file_path')
: Return the file size in bytes. -
os.stat(file).st_size
: Return the file size in bytes
Pathlib module
:
-
pathlib.Path('path').stat().st_size
: Return the file size in bytes.