0
0
Raspberry Piprogramming~10 mins

Data rotation and cleanup in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Data rotation and cleanup
Start
Check data size or age
Is data too old or large?
NoWait or Exit
Yes
Rotate data: archive or move old data
Cleanup: delete or compress old files
Log actions
End or repeat
This flow checks if data is too old or large, then rotates and cleans it up to save space and keep things organized.
Execution Sample
Raspberry Pi
import os
import time

if os.path.getsize('log.txt') > 1000:
    os.rename('log.txt', f'log_{int(time.time())}.txt')
    open('log.txt', 'w').close()
This code checks if 'log.txt' is bigger than 1000 bytes, renames it with a timestamp, and creates a new empty 'log.txt'.
Execution Table
StepActionConditionResultOutput
1Check size of 'log.txt'Size > 1000?TrueSize is 1500 bytes
2Rename 'log.txt' to 'log_<timestamp>.txt'N/AFile renamedlog_1687000000.txt
3Create new empty 'log.txt'N/AFile created emptylog.txt size 0 bytes
4End processN/ACleanup doneReady for new logs
💡 Data rotated because size exceeded 1000 bytes
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
log.txt size (bytes)15001500N/A (renamed)00
log file name'log.txt''log.txt''log_<timestamp>.txt''log.txt''log.txt'
Key Moments - 2 Insights
Why do we rename the file before creating a new one?
Renaming preserves the old data by moving it to a new file, so we don't lose logs before starting fresh, as shown in step 2 of the execution table.
What happens if the file size is not greater than 1000?
The code skips renaming and cleanup, so the file stays the same. This is implied by the condition in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the size of 'log.txt' after step 3?
A1500 bytes
B1000 bytes
C0 bytes
DFile deleted
💡 Hint
Check the 'Result' and 'Output' columns in step 3 of the execution table.
At which step is the old log file renamed?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column to find when renaming happens.
If the file size was 900 bytes, what would happen?
ANo action taken
BNew empty file created
CFile would be renamed
DFile deleted
💡 Hint
Refer to the condition in step 1 about size > 1000.
Concept Snapshot
Data rotation and cleanup:
- Check data size or age
- If too large/old, rename or archive data
- Create new empty file or compress old data
- Helps keep storage clean and organized
- Common in log file management on Raspberry Pi
Full Transcript
This example shows how to manage data files on a Raspberry Pi by checking if a file is too large, then renaming it with a timestamp to save old data, and creating a new empty file for fresh data. This process is called data rotation and cleanup. It helps keep storage organized and prevents files from growing too big. The code checks the file size, renames the file if needed, and resets the original file. The execution table traces each step, showing variable changes and actions. This method is useful for managing logs or data files that grow over time.