How to get file size in Python?

We can follow various ways to deal with getting the record size in Python. Get the document size in Python to screen record size or in the event of requesting records in the registry as indicated by document size.

Python is quite possibly the most flexible programming language. With it, you’ll have the option to work from a little CLI (Command-line interface) program to an intricate web application.

Nonetheless, one of its most misjudged highlights is the capacity to associate with usable frameworks. Overseeing OS activities with Python can save you huge loads of time while making mechanization processes.

How Python collaborate with the OS?

Python cooperates with the Os, sys, way, and subprocess modules

Nobody can live separated from their surroundings. That additionally applies in Python, where in some cases is key to associate with the employable framework to finish stuff.

Python has a few modules that let us communicate with the OS. The most utilized are os, sys, pathlib, and subprocess.

Since they are underlying modules, you won’t have to introduce them with PIP. You can import every one of them with the accompanying assertion:

  • import os
  • import sys
  • import pathlib
  • import subprocess

The underneath list shows the primary usefulness of every single one of these imports:

Operating system: Portable method of utilizing framework explicit (Depending on your OS) usefulness. It is the ideal decision by and large except if you want something further developed

Sys: System-explicit boundaries and capacities. This module gives admittance to translator factors and capacities. The os module collaborates with the employable framework and sys cooperates with the Python mediator
Pathlib: Advanced way utilization. Allows you to address filesystems as articles, with the appropriate semantic for every OS.

Subprocess: Execution and subprocesses the board straightforwardly from Python. That includes working with the stdin, stdout, and bringing codes back. You can find out about it by perusing our Python subprocess guide.
There are significant level libraries that incorporate considerably more explicit usefulness relying upon your requirements. Nonetheless, more often than not you’re all set with the above modules.

Note: Most of the capacities given by these modules will have an alternate result contingent upon your OS. Recollect that typically, the best match is UNIX and Python.

Presently you have a fast handle on how Python communicates with the OS, we should bounce into the techniques for checking record and envelope size. Every one of the accompanying arrangements is accessible in the File and envelope size in the Python GitHub store

This function takes a file path as an argument and it returns the file size (bytes).

Example:

# approach 1
# using getsize function os.path module
import os
file_size = os.path.getsize('d:/file.jpg')
print("File Size is :", file_size, "bytes")

Output:

File Size is : 218 bytes

Method 2: Using stat function of the OS module

This function takes a file path as an argument (string or file object) and returns statistical details about file path given as input.

Example:

# approach 2
# using stat function of os module
import os
file_size = os.stat('d:/file.jpg')
print("Size of file :", file_size.st_size, "bytes")

Output:

Size of file : 218 bytes

Method 3: Using File Object

To get the file size, follow these steps –

  1. Use the open function to open the file and store the returned object in a variable.  When the file is opened, the cursor points to the beginning of the file.
  2. File object has seek method used to set the cursor to the desired location. It accepts 2 arguments – start location and end location. To set the cursor at the end location of the file use method os.SEEK_END.
  3. File object has tell method that can be used to get the current cursor location which will be equivalent to the number of bytes cursor has moved. So this method actually returns the size of the file in bytes.

Example:

# approach 3
# using file object
# open file
file = open('d:/file.jpg')
# get the cursor positioned at end
file.seek(0, os.SEEK_END)
# get the current position of cursor
# this will be equivalent to size of file
print("Size of file is :", file.tell(), "bytes")

Output:

Size of file is : 218 bytes

Method 4: Using Pathlib Module

The stat() method of the Path object returns st_mode, st_dev, etc. properties of a file. And, st_size attribute of the stat method gives the file size in bytes.

Example:

# approach 4
# using pathlib module
from pathlib import Path
# open file
Path(r'd:/file.jpg').stat()
# getting file size
file=Path(r'd:/file.jpg').stat().st_size
# display the size of the file
print("Size of file is :", file, "bytes")
# this code was contributed by debrc

Output:

Size of file is : 218 bytes

Also Read: How to convert an Array to String in Java?

Leave a Reply

Your email address will not be published. Required fields are marked *