ActiveState Code

Recipe 573440: Selective cleanup (deletion) of files (based on category and extension) for use by a SABnzbd+ external post-processing script


This script can be used to cleanup (delete) files with specific extensions, e.g. .sfv, .nzb, but only for downloads belonging to a particular category.

Remarks: -The cleanup is performed only in the provided directory; subdirectories inside it, if any, are not affected. -If the category name of a particular job is not set (i.e. if it is None), SABnzbd+ passes the newsgroup name of the job instead to the script.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Selective cleanup (deletion) of files (based on category and extension)
# For use by a SABnzbd+ external post-processing script

# Version: 1.03
# Date: 2008/07/06
# License: As-is; public domain
# Prerequisites: Python 2.5.2, SABnzbd+ 0.4.0

# Description: This script can be used to cleanup (delete) files with specific extensions, e.g. .sfv, .nzb, but only for downloads belonging to a particular category.

# Remarks:
# The cleanup is performed only in the provided directory; subdirectories inside it, if any, are not affected.
# If the category name of a particular job is not set (i.e. if it is None), SABnzbd+ passes the newsgroup name of the job instead to the script.

# Usage syntax:
# C:\Python25\python.exe selective_cleanup.py directory_of_job category_of_job category_specified ext1 ext2 ... extLast

# Usage examples:
# C:\Python25\python.exe "D:\My scripts\selective_cleanup.py" %1 %5 movies sfv
# C:\Python25\python.exe "D:\My scripts\selective_cleanup.py" %1 %5 "movies (hd)" sfv nzb

# Keywords:
# sabnzbd+, sabnzbd, post-processing, post-processing script
# delete, deletion, file deletion, extension, file cleanup, cleanup list

from sys import argv
import os

# Parse input arguments
directory=argv[1]
category_of_job=argv[2]
category=argv[3]
extensions=argv[4:]
del argv

# Selectively delete files
if category == category_of_job:
    files = [i for i in os.listdir(directory) if os.path.isfile(os.path.join(directory,i))]
    files = [f for f in files if os.path.splitext(f)[1][1:] in extensions]
    for f in files: os.remove(os.path.join(directory,f))

Sign in to comment