【发布时间】:2022-11-25 14:00:19
【问题描述】:
让这个看门狗查看文件夹并使用处理程序将所有新创建的文件 LPR 到特定打印机(在命令提示符批处理中定义)。问题是,当您提交大量文件时,看门狗只会处理其中的 8、9、10 或 11 个…… 我究竟做错了什么?我很确定我的“打印队列”(可能已损坏)或 Windows 处理超时有问题...
脚本是:
import os
import os.path
import subprocess
from subprocess import *
import sys
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class Watcher:
DIRECTORY_TO_WATCH = r"C:\Users\50544342\Desktop\Newfolder3\Files"
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = Handler()
self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
self.observer.start()
try:
while True:
time.sleep(5)
except:
self.observer.stop()
print("Error")
self.observer.join()
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
if event.is_directory:
# LPR print from batch on any event.
p = subprocess.Popen(['LPR.bat', event.src_path], stdout=PIPE, stderr=PIPE)
output, errors = p.communicate()
p.wait() # wait for process to terminate
elif event.event_type == 'created':
# LPR print from batch when a file is first created.
p = subprocess.Popen(['LPR.bat', event.src_path], stdout=PIPE, stderr=PIPE)
output, errors = p.communicate()
p.wait() # wait for process to terminate
if __name__ == '__main__':
w = Watcher()
w.run()
LPR.bat 内容如下:
lpr.exe -S 127.0.0.1 -P 队列 %1
提前感谢您提供的任何帮助或提示。
【问题讨论】: