Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import datetime
- import time
- from watchdog.observers import Observer
- from watchdog.events import FileSystemEventHandler
- def concatenate_files(files, output_file):
- with open(output_file, 'w') as outfile:
- for file_name in files:
- with open(file_name, 'r') as infile:
- # Read the content of each input file and write it to the output file
- outfile.write(infile.read())
- # Add a newline after each file to ensure proper separation
- outfile.write('\n')
- class MyHandler(FileSystemEventHandler):
- def on_any_event(self, event):
- # if it doesnt end with output.py
- if event.src_path.endswith('.py') and not event.src_path.endswith(output_file):
- print(f"Change detected in {
- event.src_path}. Concatenating files... ✂️")
- # Concatenate files when a change is detected
- concatenate_files(py_files, output_file)
- timestamp = datetime.datetime.now().strftime("%H:%M")
- print(f"[{timestamp}] - Files concatenated into '{
- output_file}' successfully ✅")
- if __name__ == "__main__":
- # Define the path to the main.sikuli folder
- sikuli_folder = 'main.sikuli'
- # Define the output file
- output_file = 'main.sikuli/output.py'
- if os.path.exists(output_file):
- os.remove(output_file)
- # Create an observer to monitor the main.sikuli folder
- observer = Observer()
- observer.schedule(MyHandler(), sikuli_folder, recursive=True)
- observer.start()
- try:
- while True:
- # Get all .py files from the main.sikuli folder
- py_files = [os.path.join(sikuli_folder, file) for file in os.listdir(
- sikuli_folder) if file.endswith('.py') and file != output_file]
- # Sort the file names alphabetically
- py_files.sort()
- time.sleep(1) # Sleep for 1 second to avoid high CPU usage
- except KeyboardInterrupt:
- observer.stop()
- observer.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement