Advertisement
angelhdz12

Print Content Of Files In Folder At Interval

Jun 26th, 2017
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1. import os
  2. from threading import Timer
  3. from os.path import isfile, join, exists
  4. import shutil, time
  5.  
  6. #Constants
  7. PATH_TO_WATCH = "C:\\Users\\Angel\\Documents\\Programming\\Python\\files\\files"
  8.  
  9. #Variables
  10.  
  11. #FileReader class
  12. class FileReader:
  13.  
  14.     def __init__(self, path = None):
  15.         self.path = path
  16.         self.running = False
  17.         self.timer = None
  18.  
  19.     def printFilesData(self, files):
  20.         if self.path:
  21.             #Create a timestamp for the folder that will hold our files
  22.             timestamp = time.strftime("%Y%m%d%H%M%S")
  23.             #Generate the path for the new folder
  24.             destination = join(self.path, timestamp)
  25.  
  26.             for index, file in enumerate(files):
  27.                 #Check if the current file of the iteration is really a file
  28.                 if isfile(join(self.path, file)):
  29.                     #Call readAndMoveFile()  method to print the file name, the data,
  30.                     #and and move the file to its destination.
  31.                     self.readAndMoveFile({"dest": destination, "name": file, "data": open(join(self.path, file))})
  32.  
  33.     def readAndMoveFile(self, fileData):
  34.         print("\nName:%s\n\n%s\n" % (fileData["name"], fileData["data"].read()))
  35.         #Important! Close the file, so we can move it, otherwise, we'll get an error that the file is in use.
  36.         fileData["data"].close()
  37.         #Create timestamped folder if not exists
  38.         if not exists(fileData["dest"]):
  39.             os.makedirs(fileData["dest"])
  40.         try:
  41.             #Move the file in
  42.             shutil.move(join(self.path, fileData["name"]), fileData["dest"])
  43.             print("\nFile \"%s\" moved to \"%s\" successfully.\n" % (fileData["name"], fileData["dest"]))
  44.         except WindowsError as e:
  45.             #If there is an error moving the file, will know here.
  46.             print(e)
  47.  
  48.     def listFiles(self):
  49.         if self.path:
  50.             return [file for file in os.listdir(self.path)]
  51.         return None
  52.  
  53.     def stopWatching(self):
  54.         print("\nNot watching.\n")
  55.         self.timer.cancel()
  56.  
  57.     def complete(self):
  58.         #Timer finished.
  59.         after = self.listFiles()
  60.         added = [file for file in after if not file in self.lock]
  61.         self.printFilesData(added)
  62.         self.lock = after
  63.         self.timer = Timer(5.0, self.complete)
  64.         self.timer.start()
  65.  
  66.     def startWatching(self):
  67.         if(self.path):
  68.             self.lock = self.listFiles()
  69.             print("\nWatching path %s...\n" % self.path)
  70.             self.printFilesData(self.lock)
  71.             self.timer = Timer(5.0, self.complete)
  72.             self.timer.start()
  73.  
  74.         else: print("Path not set.")
  75.  
  76. #Main class
  77. class Main():
  78.  
  79.     def __init__(self):
  80.         #Create an instance of FileReader class, passing the path to watch as argument
  81.         self.reader = FileReader(PATH_TO_WATCH)
  82.         #Start watching the path
  83.         self.reader.startWatching()
  84.  
  85.  
  86. if __name__ == '__main__':
  87.     Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement