Counts the number of files inside each subfolder of your specified directory.
If there are more files in a subfolder than the allowed number set when running the script, they are deleted,
in order of creation date (oldest first).
I use the container based service Podgrab to automatically download new episodes.
And my PyPlayPodcast script to start playback by voice.
- 1: Create the shell command.
Create the file 'shell_command.yaml' file in your /config dir and paste in the code below.
(dont forget to include it in configuration.yaml
like shell_command: !include shell_command.yaml
.)
This will allow you to call the script easily later from for example an automation.
Here we specify all the information that the python script needs to be run.
Chose your folder where your podcasts folders are, the maximum number of files, and the fileextension.
- 2: Python script
This is where the magic happends.
Within your /config dir, create a file called pypodcleanup.py
Paste in the code from below.
It has commented sections so you can more easy understand how it is being run.
- 3: Example automation
This is an example of an automation to run the script each day.
I would suggest that you edit or make your own after you specifc needs.
Your all set, try it out!
This script is written for a folder structure like:
/media/Podcasts/PodcastName/PodcastFiles.mp3
py_podcleanup: "python pypodcleanup.py '/media/Podcasts' 10 mp3"
import os
import sys
import glob
import time
def cleanup_subfolders(folder_path, max_files, file_extension):
# Iterate through each subfolder in the specified folder
for subdir, _, _ in os.walk(folder_path):
# Filter files by extension
files = glob.glob(os.path.join(subdir, f"*.{file_extension}"))
# Check if the number of files exceeds the maximum allowed
if len(files) > max_files:
# Sort files by creation time (oldest first)
files.sort(key=lambda x: os.path.getctime(x))
# Calculate the number of files to delete
files_to_delete_count = len(files) - max_files
# Delete the oldest files
for i in range(files_to_delete_count):
os.remove(files[i])
print(f"Deleted: {files[i]}")
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: python script.py folder_path max_files file_extension")
sys.exit(1)
folder_path = sys.argv[1]
max_files = int(sys.argv[2])
file_extension = sys.argv[3]
cleanup_subfolders(folder_path, max_files, file_extension)