Advertisement
Guest User

Untitled

a guest
May 11th, 2024
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. import os
  2. import datetime
  3. import time
  4. from watchdog.observers import Observer
  5. from watchdog.events import FileSystemEventHandler
  6.  
  7.  
  8. def concatenate_files(files, output_file):
  9.     with open(output_file, 'w') as outfile:
  10.         for file_name in files:
  11.             with open(file_name, 'r') as infile:
  12.                 # Read the content of each input file and write it to the output file
  13.                 outfile.write(infile.read())
  14.                 # Add a newline after each file to ensure proper separation
  15.                 outfile.write('\n')
  16.  
  17.  
  18. class MyHandler(FileSystemEventHandler):
  19.     def on_any_event(self, event):
  20.         # if it doesnt end with output.py
  21.         if event.src_path.endswith('.py') and not event.src_path.endswith(output_file):
  22.             print(f"Change detected in {
  23.                  event.src_path}. Concatenating files... ✂️")
  24.             # Concatenate files when a change is detected
  25.             concatenate_files(py_files, output_file)
  26.             timestamp = datetime.datetime.now().strftime("%H:%M")
  27.             print(f"[{timestamp}] - Files concatenated into '{
  28.                  output_file}' successfully ✅")
  29.  
  30.  
  31. if __name__ == "__main__":
  32.     # Define the path to the main.sikuli folder
  33.     sikuli_folder = 'main.sikuli'
  34.  
  35.     # Define the output file
  36.     output_file = 'main.sikuli/output.py'
  37.  
  38.     if os.path.exists(output_file):
  39.         os.remove(output_file)
  40.  
  41.     # Create an observer to monitor the main.sikuli folder
  42.     observer = Observer()
  43.     observer.schedule(MyHandler(), sikuli_folder, recursive=True)
  44.     observer.start()
  45.  
  46.     try:
  47.         while True:
  48.             # Get all .py files from the main.sikuli folder
  49.             py_files = [os.path.join(sikuli_folder, file) for file in os.listdir(
  50.                 sikuli_folder) if file.endswith('.py') and file != output_file]
  51.             # Sort the file names alphabetically
  52.             py_files.sort()
  53.             time.sleep(1)  # Sleep for 1 second to avoid high CPU usage
  54.     except KeyboardInterrupt:
  55.         observer.stop()
  56.     observer.join()
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement