0
0
Raspberry Piprogramming~30 mins

Data rotation and cleanup in Raspberry Pi - Mini Project: Build & Apply

Choose your learning style9 modes available
Data rotation and cleanup
📖 Scenario: You are managing a Raspberry Pi that collects sensor data every day. To save space, you want to keep only the latest 5 days of data files and remove older ones automatically.
🎯 Goal: Build a Python script that lists data files, keeps only the latest 5 files, and deletes older files to clean up storage.
📋 What You'll Learn
Create a list of data file names with dates in the format 'data_YYYYMMDD.txt'.
Set a variable to keep track of how many recent files to keep.
Use a sorting method to find the latest files and select only those to keep.
Print the list of files that will be deleted.
💡 Why This Matters
🌍 Real World
This script helps Raspberry Pi users manage limited storage by automatically cleaning old sensor data files.
💼 Career
Understanding file management and cleanup scripts is useful for system administrators and IoT developers who maintain devices with limited storage.
Progress0 / 4 steps
1
Create the list of data files
Create a list called data_files with these exact file names: 'data_20230420.txt', 'data_20230421.txt', 'data_20230422.txt', 'data_20230423.txt', 'data_20230424.txt', 'data_20230425.txt', 'data_20230426.txt'.
Raspberry Pi
Need a hint?

Use square brackets to create a list and put each file name as a string inside.

2
Set the number of files to keep
Create a variable called keep_count and set it to 5 to keep the latest 5 files.
Raspberry Pi
Need a hint?

Just assign the number 5 to the variable keep_count.

3
Select files to delete
Sort the data_files list in ascending order and create a new list called files_to_delete that contains all files except the last keep_count files.
Raspberry Pi
Need a hint?

Use the sorted() function to sort the list. Use slicing to get all but the last keep_count files.

4
Print files to delete
Print the files_to_delete list to show which files will be deleted.
Raspberry Pi
Need a hint?

Use print(files_to_delete) to show the list.