-
Notifications
You must be signed in to change notification settings - Fork 2
/
listen.py
56 lines (44 loc) · 1.72 KB
/
listen.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
import win32file
import win32con
import os
import subprocess
# Start the subprocess
subprocess_args = ['./a.exe'] # Replace with your own command and arguments
subprocess_instance = subprocess.Popen(subprocess_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
# Define the path to the file you want to monitor
file_path = "input.txt"
# Open the file
file_handle = win32file.CreateFile(
file_path,
win32con.GENERIC_READ,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE,
None,
win32con.OPEN_EXISTING,
win32con.FILE_ATTRIBUTE_NORMAL,
None,
)
# Read the initial content of the file
previous_content = win32file.ReadFile(file_handle, 4096)[1].decode("utf-8")
# Start monitoring the file for changes
change_detected = False
while True:
# Read the current content of the file
current_content = win32file.ReadFile(file_handle, 4096)[1].decode("utf-8")
# Normalize line endings for comparison
previous_content = previous_content.replace(os.linesep, "\n")
current_content = current_content.replace(os.linesep, "\n")
# Compare the current content with the previous content
if current_content != previous_content and not change_detected:
print("File content changed")
print(current_content)
# Send input to the subprocess
user_input = f"{(current_content)}\n"
subprocess_instance.stdin.write(user_input.encode())
subprocess_instance.stdin.flush()
change_detected = True
elif current_content == previous_content:
change_detected = False
# Update the previous content with the current content
previous_content = current_content
# Close the file handle
win32file.CloseHandle(file_handle)