Jul-03-2019, 06:54 PM
(Jul-03-2019, 02:24 PM)redwood Wrote: As you know that I need to run the subprocess.Popen() on the latest file. So I assume that I would have to call it underYes you call it under
"def file_compare" function within the "else" block as it the part that is working on the latest file. Or do I need to use that "schedule" module?
file_compare
function.You do not need to use the schedule can just run
watch_folder.py
alone or use Cron.Example:
import subprocess out = subprocess.run(['ping', 'google.com'], capture_output=True) print(out.stdout.decode())
Output:Pinging google.com [172.217.21.174] with 32 bytes of data:
Reply from 172.217.21.174: bytes=32 time=77ms TTL=57
Reply from 172.217.21.174: bytes=32 time=62ms TTL=57
Reply from 172.217.21.174: bytes=32 time=69ms TTL=57
Reply from 172.217.21.174: bytes=32 time=68ms TTL=57
Ping statistics for 172.217.21.174:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 62ms, Maximum = 77ms, Average = 69ms
So if i want to run this in code eg each time a new .txt file is made or can as test use run.txt
to activate.def file_compare(file_stamp, file_name, log_folder): # 3 rotations, each with a maximum filesize of 1MB logzero.logfile(f"{log_folder}logfile.log", maxBytes=1e6, backupCount=3, disableStderrLogger=True) try: with open(f'{log_folder}stamp.txt') as f: old_stamp = float(f.read()) if old_stamp == file_stamp: print(f'No change: {file_name} --> {file_stamp}') else: if file_name == 'run.txt': output = subprocess.run(['ping', 'google.com'], capture_output=True) print(output.stdout.decode()) print(f'New file: {repr(file_name)} --> {file_stamp}') logger.info(f'{file_name} --> {file_stamp}') with open(f'{log_folder}stamp.txt', 'w') as f: f.write(str(file_stamp)) except OSError: with open(f'{log_folder}stamp.txt', 'w') as f: f.write(str(file_stamp))In setup i watch folder eggs and .txt files only.
folder_to_watch = 'E:/eggs' file_extension = 'txt'So to test i use schedule(run
watch_folder.py
every 10-sec) and i make .txt files live.As soon as i make
run.txt
the subprocess code will run.![[Image: ktPF6u.jpg]](https://imagizer.imageshack.com/v2/xq90/924/ktPF6u.jpg)