python - 监视文件夹和打印 - 不使用 "endless"while 循环的替代方法?

标签 python printing monitoring pywinauto

所以,我写这个是为了监视文件夹中的新图片并打印找到的任何图片。它有效,但我假设有一种更强大/更有效的方法来解决这个问题,因为我希望它一次运行 5-6 小时。

我的主要问题是我不喜欢使用像这样的“open”while 循环....

有人会以不同的方式解决这个问题吗?如果是的话,有人愿意解释一下吗?

import os
import glob
import win32com.client
import time
from pywinauto.findwindows    import find_window
from pywinauto.win32functions import SetForegroundWindow

printed = []
i = 10 

while i < 1000000000000000:

files = glob.glob("C://Users//pictures/*.jpg")
for filename in files:
    print filename
    try:
        if printed.index(str(filename)) >= 0:
            print printed.index(filename)
            print "Image found" 
    except ValueError:  
        printed.append(filename)  
        os.startfile(filename, "print")
        shell = win32com.client.Dispatch("WScript.Shell")
        time.sleep(2)
        SetForegroundWindow(find_window(title='Print Pictures'))   
        shell.AppActivate("Print Pictures")
        shell.SendKeys("{ENTER}")

i = i + 1
time.sleep(5)

最佳答案

下面的链接是相关帖子。您可以使用观察者来触发您的操作,而不是使用长 while 循环。

How to detect new or modified files

关于python - 监视文件夹和打印 - 不使用 "endless"while 循环的替代方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34425728/

相关文章:

elasticsearch - Thruk业务流程数据导出到弹性堆栈中

python - macos 10.10导入MySQLdb错误

python - 在多个文件中搜索多个正则表达式,然后输出每个匹配项及其各自的文件

python - "continue"是逃离 try catch block 的 Pythonic 方式吗?

python - 将输入的以空格分隔的项目添加到字典中。 Python

html - 打印样式表+链接

html - 表格不会打印背景或字体颜色

c# - 使用 Interop 打印 Excel

mysql - 如何确定日志中剩余的数据量

elasticsearch - 使用Kafka,Storm和Elasticsearch构建数据管道