1

So, i wrote this to monitor a folder for new pictures and print any that are found. It works, but I am assuming there is a more robust/efficient way to tackle this problem as I want it to run for 5-6 hours at a time.

My main problem is that I don't like using "open" while loops like this....

Would anyone tackle this differently? If so, would anyone be willing to explain?

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)
Ciaran
  • 436
  • 1
  • 7
  • 21
  • 1
    Check here [http://stackoverflow.com/questions/182197/how-do-i-watch-a-file-for-changes-using-python](http://stackoverflow.com/questions/182197/how-do-i-watch-a-file-for-changes-using-python) – scope Dec 22 '15 at 22:57
  • That works perfectly, thank you very much! I learned a lot from that link :) – Ciaran Dec 22 '15 at 23:31

2 Answers2

1

link below is related post. instead of using a long while loop you can use a watcher to trigger your operation.

How to detect new or modified files

Community
  • 1
  • 1
0

Big thanks to scope for his comment, i have added my printing lines to the example and it works well. Code posted below for anyone who wants it, commented code is in the link code posted. Now to tidy up a few other things....

import os

import win32file
import win32event
import win
import glob
import win32com.client
import time
from pywinauto.findwindows    import find_window
from pywinauto.win32functions import SetForegroundWindow




def print_photo(filename): 
    print filename
    filename = path_to_watch +"\\" + filename[0]
    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}")

path_to_watch = os.path.abspath ("C:\\Users\\Ciaran\\Desktop\\")


change_handle = win32file.FindFirstChangeNotification (
  path_to_watch,
  0,
  win32con.FILE_NOTIFY_CHANGE_FILE_NAME
)


try:

  old_path_contents = dict ([(f, None) for f in os.listdir     (path_to_watch)])
  while 1:
    result = win32event.WaitForSingleObject (change_handle, 500)


    if result == win32con.WAIT_OBJECT_0:
      new_path_contents = dict ([(f, None) for f in os.listdir     (path_to_watch)])
      added = [f for f in new_path_contents if not f in     old_path_contents]
      print_photo(added)
      deleted = [f for f in old_path_contents if not f in new_path_contents]
      if added: print "Added: ", ", ".join (added)
      if deleted: print "Deleted: ", ", ".join (deleted)

      old_path_contents = new_path_contents
      win32file.FindNextChangeNotification (change_handle)

finally:
  win32file.FindCloseChangeNotification (change_handle)
Ciaran
  • 436
  • 1
  • 7
  • 21