File Sorting and Filtering with Regex and Random String Generation

  • Share this:

Code introduction


This function accepts two arguments: a directory path and a regular expression pattern. It generates a random string, then finds all files in the directory and sorts them by size, finally returning a list of files that match the pattern, total number of files, number of filtered files, a random string, and execution time.


Technology Stack : os, sys, json, time, random, re, string

Code Type : Function

Code Difficulty : Intermediate


                
                    
import os
import sys
import json
import time
import random
import re
import string

def generate_random_string(length=10):
    return ''.join(random.choices(string.ascii_letters + string.digits, k=length))

def find_files_in_directory(directory):
    for root, dirs, files in os.walk(directory):
        for file in files:
            yield os.path.join(root, file)

def sort_files_by_size(directory):
    files = list(find_files_in_directory(directory))
    return sorted(files, key=lambda x: os.path.getsize(x), reverse=True)

def xxx(arg1, arg2):
    start_time = time.time()
    random_str = generate_random_string(15)
    sorted_files = sort_files_by_size(arg1)
    pattern = re.compile(arg2)
    
    filtered_files = [file for file in sorted_files if pattern.search(file)]
    results = {
        "total_files": len(sorted_files),
        "filtered_files": len(filtered_files),
        "random_string": random_str,
        "execution_time": time.time() - start_time
    }
    
    return results

# Example usage:
# result = xxx('/path/to/directory', 'pattern_to_search')
# print(result)