Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from threading import Timer
- from os.path import isfile, join, exists
- import shutil, time
- import logging
- logging.basicConfig(filename='C:\Users\BetaBrawler\Downloads\Informe\Informe.txt', level=logging.DEBUG,
- format='%(asctime)s:%(levelname)s:%(message)s')
- PATH_TO_WATCH = "C:\Users\BetaBrawler\Downloads"
- class FileReader:
- def __init__(self, path = None):
- self.path = path
- self.running = False
- self.timer = None
- def printFilesData(self, files):
- if self.path:
- timestamp = time.strftime("%d-%m-%Y-%H-%M-%S")
- destination = join(self.path, timestamp)
- for index, file in enumerate(files):
- if isfile(join(self.path, file)):
- self.readAndMoveFile({"dest": destination, "name": file, "data": open(join(self.path, file))})
- def readAndMoveFile(self, fileData):
- logging.debug("Archivo:%s%s" % (fileData["name"], fileData["data"].read()))
- #El archivo debe ser cerrado para que se mueve
- fileData["data"].close()
- if not exists(fileData["dest"]):
- os.makedirs(fileData["dest"])
- try:
- shutil.move(join(self.path, fileData["name"]), fileData["dest"])
- logging.debug("Archivo \"%s\" movido \"%s\" ." % (fileData["name"], fileData["dest"]))
- except WindowsError as e:
- logging.warning(e)
- def listFiles(self):
- if self.path:
- return [file for file in os.listdir(self.path)]
- return None
- def stopWatching(self):
- logging.warning("Not watching.")
- self.timer.cancel()
- def complete(self):
- after = self.listFiles()
- added = [file for file in after if not file in self.lock]
- self.printFilesData(added)
- self.lock = after
- self.timer = Timer(5.0, self.complete)
- self.timer.start()
- def startWatching(self):
- if(self.path):
- self.lock = self.listFiles()
- logging.debug("Directorio que se esta observando %s..." % self.path)
- self.printFilesData(self.lock)
- self.timer = Timer(5.0, self.complete)
- self.timer.start()
- else: logging.warning("Ruta sin definir.")
- class Main():
- def __init__(self):
- self.reader = FileReader("C:\Users\BetaBrawler\Downloads")
- self.reader.startWatching()
- if __name__ == '__main__':
- Main()
- #Debug: Seguimiento mas acusioso
- #Info: Informacion
- #Warning
- #Critical
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement