Python 3 Script to Get List of Files in Directory & Show the Size of Each File in Terminal
287 views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-script-to-get-list-of-files-in-directory-show-the-size-of-each-file-in-terminal/
View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
so in this video I will show you how to
0:05
get the list of all the files which are
0:07
present in a specific directory and also
0:09
show the appropriate size of each file
0:12
side by side so as you can see we are
0:15
running this Python script and in the
0:17
home directory we are searching for all
0:19
the files displaying it this is a file
0:22
name followed by the size name sorry
0:25
size as well in
0:28
megabyte so if you want the output to be
0:31
in this format I will show you this
0:33
Python script which will loop through
0:34
all the files and display the size as
0:37
well side by side you can target any
0:39
directory and see the result like this
0:42
in the
0:43
terminal i have given the script in the
0:45
description of the video so now let me
0:47
show you step by step how I built this
0:51
so so first of all you need to import
0:55
the operating system module which is a
0:57
built-in module and then here we specify
1:01
which directory that we are targeting so
1:03
we are targeting the home directory dot
1:07
/ and then we use this lambda
1:13
function operating system
1:18
path so here we are checking that if
1:21
it's a file or not and joining the
1:30
path so after that we will get the list
1:33
of files we will filter
1:36
it using this lambda
1:40
function list directory which will list
1:42
out all the files for us
1:46
so after getting the list of files we
1:50
will
1:53
simply also get the file size for
1:56
getting the file size we simply use
2:02
this this
2:14
function so we get the size here using
2:16
this
2:19
property and then we loop through all
2:22
the files here like this
2:25
so this basically stores all the size
2:27
and then we loop
2:33
through and we actually print out the
2:35
size here like
2:38
this i've given the full
2:43
script so we're rounding it out to get
2:45
the result in megabytes so that's all
2:48
that is needed if I again execute this
2:51
you will see that it is saying filter
2:53
fun is not defined
2:59
uh it is on line number
3:05
seven
3:07
filter okay so this
3:21
is n path has no attribute on line
3:25
number
3:39
lights so you can see it actually
3:42
returned the list of files alongside
3:45
with their size as well in
3:48
megabytes so the full script is given in
3:50
the description thank you very much for
3:52
watching this video and also check out
3:54
my website freemediatools.com
3:58
uh which contains thousands of tools
#Computers & Electronics
#Programming
#Software