-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPurge_Dirs.py
58 lines (51 loc) · 1.91 KB
/
Purge_Dirs.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import re
import os
import sys
import shutil
import logging
DIR = "/Users/Cibeles/PycharmProjects/MelaUtils/Descargas"
LOGFILE = "/Users/Cibeles/PycharmProjects/MelaUtils/flexget/flexget.log"
LOGFORMAT = "[%(asctime)s - %(levelname)s] %(message)s"
#DIR = "/Volumes/Vault/Descargas/"
#LOGFILE = "/Volumes/Vault/flexget/flexget.log"
nombre_dir = os.listdir(DIR)
fobj = sys.stdout
ftype_reg_exp = re.compile('.*\.(avi|mkv|mp4|mpg|mpeg)$')
formatter = logging.Formatter(LOGFORMAT)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# file handler
file = logging.FileHandler(LOGFILE)
#file.setLevel(logging.DEBUG)
file.setFormatter(formatter)
logger.addHandler(file)
# console handler
console = logging.StreamHandler()
#console.setLevel(logging.DEBUG)
console.setFormatter(formatter)
logger.addHandler(console)
try:
fobj = open(LOGFILE, 'a')
except Exception as e:
if e.errno == 2:
print "No se pueden escribir logs, el path no existe: " + e.strerror
for item in nombre_dir:
cwd = DIR + '/' + item + '/'
print "directorio " + cwd
if os.path.isdir(cwd):
directorios = os.listdir(cwd)
try:
for item in directorios:
if (os.path.isfile(cwd + item) and ftype_reg_exp.match(cwd + item)):
logger.warn("[PURGE DIRS] Directorio con archivos de video, no se borra %s" % (cwd + item))
print "directorio " + cwd + item + "con archivos de video"
raise Exception
shutil.rmtree(cwd)
print "directorio " + cwd + " vacio lo borrare"
logger.info("[PURGE DIRS] Directorio vacio o sin archivos de video, borrado %s" % cwd)
except Exception, e:
print "excepcion " + e.message
logger.error("[PURGE DIRS] %s" % e.message)
else:
print "directorio " + cwd + "estamos en el raiz, no borro"
logger.info("[PURGE DIRS] Directorio raiz de descargas %s" % cwd)