Before starting the script, first take a deep breath and exhale.
Think about what we are going to achieve. Write down the steps (this is called logic—nothing funny about it!).
We are going to find the largest file in the /etc/apache2 folder with a .conf extension. Keep thinking, and use your brain.
The first step should be to get a list of directories and files with the .conf extension. Keep thinking, and use your brain.
Oh! Python has a module that provides a detailed view of directories, subdirectories, and files in a directory. The os module! So, that's done.
The output of the os.walk() function will be in the form of a tuple. The first item is a string, and the following two items are lists.
I understand this might be confusing. Here’s a sample output for better understanding:
Example
a = {'/etc/apache2/apache2.conf': 7224, '/etc/apache2/ports.conf': 320, '/etc/apache2/mods- available/dir.conf': 157}
print(a.items())
# Output: dict_items([('/etc/apache2/apache2.conf', 7224), ('/etc/apache2/ports.conf', 320), ('/etc/apache2/mods-available/dir.conf', 157)])
The second step is to find the size of each file that ends with .conf and store it somewhere... hmm... still thinking.
Ah! In Python, we have a module called Posixpath, which helps calculate the size of files. We can use it in a for loop to iterate over each file.
How will we iterate over each file? For that, we can use a combination of the for loop and if condition. Nice... keep thinking!
We have the filename and its size, and we need to store them somewhere to sort them later.
Ah! We can use a dictionary in Python, which has a method called items() that provides both key/value pairs.
Let’s open a blank dictionary with the command D = {}. Now, D is a blank dictionary.
Add each item to the dictionary using d['key'] = value.
Now we need to create a function to sort the items based on the size of the file.
The function will accept dictionary items and process them.
-------------------------------------------------------------------------------------------------------------------------------------------------
import os
import posixpath
# Initialize a list to hold file paths and their sizes
d_size = []
# Walk through the directories and subdirectories in /etc/apache2
dir_list = os.walk('/etc/apache2/')
for Cur_dir, Sub_Dirs, Sub_files in dir_list:
# Iterate over each file in the current directory
for file in Sub_files:
# Check if the file ends with '.conf'
if file.endswith('.conf'):
abs_path = posixpath.join(Cur_dir, file)
# Ensure it's a file, not a directory
if posixpath.isfile(abs_path):
# Get the size of the file
size = posixpath.getsize(abs_path)
# Append the file path and size to the list
d_size.append([abs_path, size])
# Function to sort by file size
def sorted_size(values):
return values[1]
# Sort the list of files by size in descending order
Output = sorted(d_size, key=sorted_size, reverse=True)
# Print the sorted list of files and their sizes
for item in Output:
path, size_of_file = item
print(f'{path:60}- {size_of_file} bytes')