function file_cleanup
Removes files older than 60 seconds from the './static/files/' directory.
/tf/active/vicechatdev/datacapture_integrated.py
4091 - 4100
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
ostime
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)
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function cleanup_old_documents 65.6% similar
-
function check_static_files 62.9% similar
-
function touch_static_files 59.9% similar
-
function cleanup_old_tasks 57.4% similar
-
function cleanup_old_tasks_v1 52.2% similar