Python - Get list of files in directory with size - GeeksforGeeks

15 Sep, 2021
In this article, we are going to see how to extract the list of files of the directory along with its size. For this, we will use the OS module.
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system-dependent functionality. os.path module is a submodule of OS module in Python used for common path name manipulation.
os.path.isfile() method in Python is used to check whether the specified path is an existing regular file or not.
Syntax: os.path.isfile(path)
Parameter:  
path:  path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.
Return Type: This method returns a Boolean value of class bool. This method returns True if specified path is an existing regular file, otherwise returns False. 
os.path.join() method in Python join one or more path components intelligently. This method concatenates various path components with exactly one directory separator (‘/’) following each non-empty part except the last path component. If the last path component to be joined is empty then a directory separator (‘/’) is put at the end. If a path component represents an absolute path, then all previous components joined are discarded and joining continues from the absolute path component.
Syntax: os.path.join(path, *paths)  
Parameter:  
path:  A path-like object representing a file system path.  
*path: A path-like object representing a file system path. It represents the path components to be joined.
A path-like object is either a string or bytes object representing a path.
Note: The special syntax *args (here *paths) in function definitions in python is used to pass a variable number of arguments to
a function.
Return Type: This method returns a string which represents the concatenated path components.  
os.listdir(): This method in python is used to get the list of all files and directories in the specified directory. If we don’t specify any directory, then a list of files and directories in the current working directory will be returned.
Syntax: os.listdir(path)
Parameters:  
path (optional) : path of the directory
Return Type: This method returns the list of all files and directories in the specified path. The return type of this method is list.
filter(): This method filters the given sequence with the help of a function that tests each element in the sequence to be true or not.
Syntax: filter(function, sequence)
Parameters:
function: function that tests if each element of a sequence true or not.
sequence: sequence which needs to be filtered, it can be sets, lists, tuples, or containers of any iterators.
Returns: returns an iterator that is already filtered.
os.stat(): This method in Python performs stat() system call on the specified path. This method is used to get the status of the specified path.
Syntax: os.stat(path)
Parameter:
path: A string or bytes object representing a valid path
Returns : st_size: It represents the size of the file in bytes.
os.walk(): This generates the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at the directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).
In this part of the code, we will just get the list of files’ names and sizes. In this code, we have os.stat() function to get the size of each file, and the size will results in ‘byte’ so we have to divide the size of the file from 1024*1024 to get the size in the ‘megabytes’ for a better understanding. 
Python3
# import python modulesimport os # directory name from which# we are going to extract our files with its sizepath = "D:\Books" # Get list of all files only in the given directoryfun = lambda x : os.path.isfile(os.path.join(path,x))files_list = filter(fun, os.listdir(path)) # Create a list of files in directory along with the sizesize_of_file = [    (f,os.stat(os.path.join(path, f)).st_size)    for f in files_list]# Iterate over list of files along with size# and print them one by one.for f,s in size_of_file:    print("{} : {}mb".format(f, round(s/(1024*1024),3)))
 
 
Output:
 
2015_Book_LinearAlgebraDoneRight.pdf : 2.199mb
An Introduction to Statistical Learning - Gareth James.pdf : 8.996mb
Hand-on-ML.pdf : 7.201mb
ISLR Seventh Printing.pdf : 11.375mb
The Business of the 21st Century - Robert Kiyosaki.pdf : 8.932mb
The Elements of Statistical Learning - Trevor Hastie.pdf : 12.687mb
the_compound_effect_ebook.pdf : 5.142mb
 
In the previous code, we have only output the filenames and their corresponding sizes, but in this case, we have printed file paths instead of each file name and we have sorted it according to the size of each file name in ascending order. In this, we have to use the sorted function, to sort our file, according to its size.
 
Syntax: sorted(iterable, key=key, reverse=reverse)
Parameters:
iterable : The sequence to sort, list, dictionary, tuple etc.
key : A Function to execute to decide the order. Default is None
reverse : A Boolean. False will sort ascending, True will sort descending. Default is False
 
Python3
# import python modulesimport os # directory name from which we are# going to extract our files with its sizepath = "D:\ABC" # Get list of all files only in the given directoryfun = lambda x : os.path.isfile(os.path.join(path,x)) files_list = filter(fun, os.listdir(path)) # Create a list of files in directory along with the sizesize_of_file = [    (f,os.stat(os.path.join(path, f)).st_size)    for f in files_list]# Iterate over list of files along with size# and print them one by one.# now we have print the result by# sorting the size of the file# so, we have call sorted function# to sort according to the size of the file # created a lambda function that help us# to sort according the size of the file.fun = lambda x : x[1]  # in this case we have its file path instead of filefor f,s in sorted(size_of_file,key = fun):    print("{} : {}mb".format(os.path.join(path,f),round(s/(1024*1024),3)))
Output: 
D:\ABC\1.png : 0.022mb
D:\ABC\17.png : 0.024mb
D:\ABC\16.png : 0.036mb
D:\ABC\15.png : 0.047mb
D:\ABC\7.png : 0.074mb
D:\ABC\10.png : 0.076mb
D:\ABC\6.png : 0.09mb
D:\ABC\13.png : 0.093mb
D:\ABC\14.png : 0.097mb
D:\ABC\8.png : 0.104mb
D:\ABC\2.png : 0.115mb
D:\ABC\5.png : 0.126mb
D:\ABC\11.mp4 : 5.966mb
This code is not much different from the previous code, there is a very small change in this code, in this code, we have just print the output as file name instead of file paths, and the rest of the code is the same.
Python3
# import python modulesimport os # directory name from which we are# going to extract our files with its sizepath = "D:\ABC" # Get list of all files only in the given directoryfun = lambda x : os.path.isfile(os.path.join(path,x)) files_list = filter(fun, os.listdir(path)) # Create a list of files# in directory along with the sizesize_of_file = [    (f,os.stat(os.path.join(path, f)).st_size)    for f in files_list]# Iterate over list of files along with size# and print them one by one.# now we have print the result by# sorting the size of the file# so, we have call sorted function# to sort according to the size of the file # created a lambda function that help# us to sort according the size of the file.fun = lambda x : x[1] # in this case we have use its file name.for f,s in sorted(size_of_file,key = fun):    print("{} : {}mb".format(f,round(s/(1024*1024),3)))
Output: 
1.png : 0.022mb
17.png : 0.024mb
16.png : 0.036mb
15.png : 0.047mb
7.png : 0.074mb
10.png : 0.076mb
6.png : 0.09mb
13.png : 0.093mb
14.png : 0.097mb
8.png : 0.104mb
2.png : 0.115mb
5.png : 0.126mb
11.mp4 : 5.966mb
This code is different from all the 3 above codes, in this code, we have to show all the sub_directory and file sizes with their name or file paths. So, first, we have to get our all sub_directores and files present in the directory by using the os.walk() function, which results in a generator object containing 3 things, i.e., path, sub_directory names, and file names present in the given directory. Then we created a list of files with have their size, and next, we have to get the size of the sub_directory present in the directory. At last, we have output our code with the sorted size of file names and their sub-directories.
Syntax: os.walk(path)
Parameters:
path : The path of the directory from where we can create our directory tree.
Returns : 
root : Prints out directories only from what you specified.
dirs : Prints out sub-directories from root.
files : Prints out all files from root and directories.
Python3
# import python modulesimport os # directory name from which we are# going to extract our files with its sizepath = "D:\ABC" # get the path p, sub_directory sub_dir,# and filename files from the given pathwalk_method = os.walk(path) # using exception handling to remove# the stop iteration from generator object# which we get the output from os.walk()  method.while True:    try:        p, sub_dir, files = next(walk_method)        break    except:        break          # Create a list of files in directory along with the sizesize_of_file = [    (f,os.stat(os.path.join(path, f)).st_size)    for f in files]  # get the size of the sub_dir of the given pathfor sub in sub_dir:    i = os.path.join(path,sub)    size = 0    for k in os.listdir(i):        size += os.stat(os.path.join(i,k)).st_size    size_of_file.append((sub,size))     # Iterate over list of files along with size# and print them one by one.# now we have print the result by# sorting the size of the file# so, we have call sorted function# to sort according to the size of the file # in this case we have use its file paths.for f,s in sorted(size_of_file,key = lambda x : x[1]):    print("{} : {}mb".format(os.path.join(path,f),round(s/(1024*1024),3)))
Output:
D:\ABC\1.png : 0.022mb
D:\ABC\17.png : 0.024mb
D:\ABC\16.png : 0.036mb
D:\ABC\15.png : 0.047mb
D:\ABC\7.png : 0.074mb
D:\ABC\10.png : 0.076mb
D:\ABC\6.png : 0.09mb
D:\ABC\13.png : 0.093mb
D:\ABC\14.png : 0.097mb
D:\ABC\8.png : 0.104mb
D:\ABC\2.png : 0.115mb
D:\ABC\5.png : 0.126mb
D:\ABC\11.mp4 : 5.966mb
D:\ABC\Books : 56.532mb
as5853535
Picked
Python directory-program
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Python | Get unique values from a list
Create a directory in Python
Defaultdict in Python
Python | Pandas dataframe.groupby()