banner



How To Get File Size In Python

In this tutorial, we volition look at how to the get the file size using Python.

At that place are a number of ways to get the size of a file in Python. You tin use the os module's bone.stat() function to get a number of file statistics including its size. You can also employ the pathlib module to get the file size.

Let's look at the usage of the above-mentioned methods with the help of some examples. First, let'southward manually check the size of the file we'll be measuring in python which is a CSV file containing 5k reviews of movies.

File properties in Windows showing the file size of a CSV file.

You tin run across that the file size is approximately vi.40 MB. Permit's at present become ahead and utilize python to go the size of the very same file.

The os module in python comes with a number of useful functions to interact with the file organisation. To get the file size, you lot tin can use the os.stat() part which unlike file statistics. To specifically get the file size, apply the st_size attribute. For example, let's get the size of the above file using this method.

import bone  # the absoulte path of the file file_path = r"C:\Users\piyush\Documents\Projects\movie_reviews_data\IMDB Dataset 5k.csv" # become the file size impress(bone.stat(file_path).st_size)

Output:

6716779

Notation that we used an absolute path to the file here. You can also use a relative path. Y'all can see that we become the file size in bytes which can easily be converted to Kilobytes and Megabytes. Let'southward create a simple part to get the file size in Bytes, Kilobytes, and Megabytes.

# role to show file size in bytes, kilobytes, and megabytes def show_file_size(size):     kb = size/1024     mb = kb/1024     print("The file size in -")     print("Bytes: {}".format(size))     print("Kilobytes (KB): {0:.2f}".format(kb))     print("Megabytes (MB): {0:.2f}".format(mb))      # display the file size size = os.stat(file_path).st_size show_file_size(size)

Output:

The file size in - Bytes: 6716779 Kilobytes (KB): 6559.35 Megabytes (MB): half-dozen.41

You can run into that we get the file size as ~half dozen.41MB which is approximately equal to the size we got from the file properties in Windows.

Alternatively, you tin also utilize the bone.path.getsize() function to become the file size.

# the absoulte path of the file file_path = r"C:\Users\piyush\Documents\Projects\movie_reviews_data\IMDB Dataset 5k.csv" # get the file size print(bone.path.getsize(file_path))

Output;

6716779

Nosotros get the aforementioned upshot as nosotros did with os.stat(file_path).st_size.

For more on the os.stat() function, refer to its documentation.

You can likewise get the file size using the pathlib module. Note that this method is simply a wrapper around os.stat(file_path) equally it returns the same object.

from pathlib import Path  # the absoulte path of the file file_path = r"C:\Users\piyush\Documents\Projects\movie_reviews_data\IMDB Dataset 5k.csv" # go the file size print(Path(file_path).stat().st_size)

Output:

6716779

We go the file size in bytes.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with python (version 3.eight.iii) kernel.


Subscribe to our newsletter for more than informative guides and tutorials.
Nosotros do non spam and you can opt out whatsoever fourth dimension.

Tutorials on interacting with the file system in Python –

  • Python – Get Filename from Path with Examples
  • Get File size using Python
  • List of all files in a directory using Python
  • Get the Current Working Directory in Python

How To Get File Size In Python,

Source: https://datascienceparichay.com/article/get-file-size-using-python/

Posted by: pardonound1973.blogspot.com

0 Response to "How To Get File Size In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel