🔍 Code Extractor

function file_cleanup

Maturity: 38

Removes files older than 60 seconds from the './static/files/' directory.

File:
/tf/active/vicechatdev/datacapture_integrated.py
Lines:
4091 - 4100
Complexity:
simple

Purpose

This function performs automatic cleanup of temporary files in a static files directory. It walks through the './static/files/' folder, checks the age of each file based on its modification time, and deletes any files that are older than 60 seconds. This is typically used in web applications to prevent accumulation of temporary files, such as user uploads, generated reports, or cached data that should not persist long-term.

Source Code

def file_cleanup(*args):
    import os
    import time
    folder_path = './static/files/'
    for folder_name, subfolders, filenames in os.walk(folder_path):
        for file in filenames:
            file_path = folder_path + file
            file_age = time.time() - os.path.getmtime(file_path)
            if file_age > 60:
                os.remove(file_path)

Parameters

Name Type Default Kind
*args - - var_positional

Parameter Details

*args: Variable length argument list that is accepted but not used in the function. The function does not utilize any parameters passed to it, making this parameter list effectively unused.

Return Value

This function does not return any value (implicitly returns None). It performs file deletion operations as side effects.

Dependencies

  • os
  • time

Required Imports

import os
import time

Usage Example

import os
import time

# Ensure the directory exists
os.makedirs('./static/files/', exist_ok=True)

# Create a test file
test_file = './static/files/test.txt'
with open(test_file, 'w') as f:
    f.write('test content')

# Wait for file to age (for testing, you'd wait 61+ seconds)
# time.sleep(61)

# Run cleanup to remove old files
file_cleanup()

# Can be called with any arguments (they are ignored)
file_cleanup('arg1', 'arg2')  # Arguments have no effect

Best Practices

  • Ensure the './static/files/' directory exists before calling this function to avoid FileNotFoundError
  • Be cautious with the hardcoded 60-second threshold - files are deleted very quickly, which may not be suitable for all use cases
  • Consider adding error handling for permission issues or files that cannot be deleted
  • The function uses os.walk() but only processes the top-level directory due to how file_path is constructed (folder_path + file)
  • Consider making the folder path and age threshold configurable parameters instead of hardcoded values
  • Add logging to track which files are deleted for debugging and audit purposes
  • Consider adding exception handling around os.remove() to prevent the function from crashing if a file is locked or already deleted
  • The *args parameter serves no purpose and could be removed for clarity
  • This function is suitable for scheduled execution (e.g., via cron job, background task, or periodic callback)

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function cleanup_old_documents 65.6% similar

    Periodically removes documents and their associated files that are older than 2 hours from the uploaded_documents dictionary, cleaning up both file system storage and memory.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function check_static_files 62.9% similar

    Scans a 'static' directory for CSS and JavaScript files and prints their modification times relative to the current time.

    From: /tf/active/vicechatdev/vice_ai/dev_tools.py
  • function touch_static_files 59.9% similar

    Updates the modification timestamp of CSS and JavaScript files in a static directory to force browser cache refresh.

    From: /tf/active/vicechatdev/vice_ai/dev_tools.py
  • function cleanup_old_tasks 57.4% similar

    Removes tasks from the active_tasks dictionary that are older than 1 hour (3600 seconds) based on their creation timestamp.

    From: /tf/active/vicechatdev/docchat/app.py
  • function cleanup_old_tasks_v1 52.2% similar

    Removes tasks from the active_tasks dictionary that are older than 1 hour (3600 seconds) based on their creation timestamp, using thread-safe locking.

    From: /tf/active/vicechatdev/vice_ai/app.py
← Back to Browse