How to Get File Size in Python?
Tags: python, programming, codenewbie, tutorial
Published: 2021-09-28 11:10:43

---
title: How to Get File Size in Python?
published: true
date: 2021-09-28 08:06:38 UTC
tags: #python #programming #codenewbie #tutorial
canonical_url: https://itsmycode.com/how-to-get-file-size-in-python/
---

ItsMyCode |

There are different ways to get file size in Python. We will be using the os module and the **`pathlib `** module to check the file size. **OS** module in Python comes as built-in, and it provides various utility methods to interact with operating system features. 

## Python get file size

The popular ways to fetch file size in Python are as follows.

- **os.path.getsize()**
- **os.stat()**
- **seek() and tell()**
- **path.stat().st\_mode**

## Method 1: Get file size using os.path.getsize()

The **`os.path.getsize()`** function takes a file path as an argument and returns the file size in bytes. If the function cannot find the file or is inaccessible, Python will raise an [OSError](https://docs.python.org/3/library/exceptions.html#OSError). 

```python 
# Import os module
import os

# set the file path
file = "python.pdf"

# Get the file size using os.path.getsize() function
file_size = os.path.getsize(file)

print('File Size in Bytes is ', file_size)
print('File Size in KiloBytes is ', (file_size / 1024))
print('File Size in MegaBytes is ', (file_size / (1024 * 1024)))

```

**Output**

```python 
File Size in Bytes is 12271318
File Size in KiloBytes is 11983.708984375
File Size in MegaBytes is 11.702840805053711
```

## Method 2: Get file size using os.stat()

The **`os.stat()`** function takes a file path as an argument and returns the statistical details of the file as a tuple. The **`stat()`** method will get the status of the specified file path, and the **`st_size`**  attribute will fetch the file size in bytes.

```python 
# Import os module
import os

# set the file path
file = "python.pdf"

#If you want to print the file info 
file_info= os.stat(file)
print(file_info)

# Get the file size using os.stat() function
file_size = os.stat(file).st_size

print('File Size in Bytes is ', file_size)
print('File Size in KiloBytes is ', (file_size / 1024))
print('File Size in MegaBytes is ', (file_size / (1024 * 1024)))

```

**Output**

```python 
os.stat_result(st_mode=33206, st_ino=12103423998770118, st_dev=3351013, st_nlink=1, st_uid=0, st_gid=0, st_size=12271318, st_atime=1632686420, st_mtime=1632608049, st_ctime=1632686420)
File Size in Bytes is 12271318
File Size in KiloBytes is 11983.708984375
File Size in MegaBytes is 11.702840805053711
```

## Method 3: Get file size using seek() and tell()

The other methods perfectly work in the case of an actual file, and if you have something like file-like objects, you could use **`seek()`** and **`tell()`** to fetch the file size.

There are three steps involved to get the file size.

**Step 1:**  Open the file using the **`open()`** function and store the return object into a variable. When the file is opened, the cursor always points to the beginning of the file.

**Step 2:**  File objects provide a **`seek()`** method to set the cursor into the desired location. It accepts two arguments start position and the end position. To set the cursor at the end location of the file, use method _ **`os.SEEK_END`**._

**Step 3:**  The file object has a **`tell()`** method that can get the current cursor position and provides the number of bytes it has moved from the initial position. Basically, it gives the actual file size in bytes format.

```python 

# Import os module
import os

# set the file path
file_name = "python.pdf"
 
# open file using open() function
file = open(file_name)
 
# set the cursor position to end of file
file.seek(0, os.SEEK_END)

# get the current position of cursor
# this will be equivalent to size of file
file_size= file.tell()
 

print('File Size in Bytes is ', file_size)
print('File Size in KiloBytes is ', (file_size / 1024))
print('File Size in MegaBytes is ', (file_size / (1024 * 1024)))
```

**Output**

```python 
File Size in Bytes is 12271318
File Size in KiloBytes is 11983.708984375
File Size in MegaBytes is 11.702840805053711
```

## Method 4: Get file size using path.stat().st\_mode

The **`stat()`** method of the Path object returns the properties of a file like  **`st_mode`** ,  **`st_dev`** , etc. and, the **`st_size `** attribute of the stat method returns the file size in bytes.

```python 
# Import pathlib module
import pathlib

# set the file path
file = "python.pdf"

# Get the file size using pathlib.Path() function
file_size = pathlib.Path(file).stat().st_size

print('File Size in Bytes is ', file_size)
print('File Size in KiloBytes is ', (file_size / 1024))
print('File Size in MegaBytes is ', (file_size / (1024 * 1024)))

```

**Output**

```python 
File Size in Bytes is 12271318
File Size in KiloBytes is 11983.708984375
File Size in MegaBytes is 11.702840805053711
```

**Note:**  The **`pathlib `** module is available only from  **Python 3.4 and above ** versions.

All the above methods provide the file size in bytes format. In most cases, if the file size is significant, you would need it in a human-readable format as kilobytes or megabytes.

**Python get file size in kb (KiloBytes)**

To convert from **bytes to Kilobytes** , divide the filesize bytes by 1024, as shown in the above examples.

**Python get file size in kb (MegaBytes)**

To convert from **bytes to Megabytes** , divide the filesize bytes by (1024 x 1024) as shown in the above examples.

The post [How to Get File Size in Python?](https://itsmycode.com/how-to-get-file-size-in-python/) appeared first on [ItsMyCode](https://itsmycode.com).