Python Program to List all the Files in a Directory with Size

In this example, we will write a python program to get the size of the files inside a directory. To better understand this example, make sure you have knowledge of the following tutorials:-

Loading...

The code for the program is as below:-

import os

def file_size(fname):
    info = os.stat(fname)
    return info.st_size


for root, dirs, files in os.walk("."):
    for filename in files:
        print(filename + ' : ' + str(file_size(filename)) + " bytes")

The output of the above program is:-

test.py : 278 bytes
about.txt : 293 bytes
hello.txt : 81 bytes

Loading...
SHARE Python Program to List all the Files in a Directory with Size

You may also like...

Leave a Reply

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

Share