title: PyAutoGui crashes when if/then statements are included
body: <p>I had a successful trial run with PyAutoGui recently with some crude code I wrote to help me complete some repetitive, monotonous uploads I have to do regularly for my wife's business. In spite of its ugliness, it worked well.</p>
  
  <p>In an attempt to refine it a bit, I began receiving error messages only after I included an if/then statement. I added the if/then statement so that it would skip over directories that were empty. I'm trying to determine what the problem is exactly. I'm using Python 3.4 on Windows 10.</p>
  
  <p>Here's the code (I used the time.sleep() command because some commands take a while to perform, especially the ones inside the browser):</p>
  
  <pre><code>import pyautogui
  from tkinter import *
  import time
  import os
  master = Tk()
  
  def Upload_All_Amelias():
      Select_Chrome()
      time.sleep(1)
      if os.listdir('D:\\Inventory\\Amelia\\XXS') != []:  #Checks to see if the directory is empty
          XXS_Amelias()
      else:
          pyautogui.hotkey('Ctrl', 'Tab'), #Selects the Next tab
      time.sleep(1)
      if os.listdir('D:\\Inventory\\Amelia\\XS') != []:  #Checks to see if the directory is empty
          XS_Amelias()
      else:
          pyautogui.hotkey('Ctrl', 'Tab'), #Selects the Next tab
      time.sleep(1)
      if os.listdir('D:\\Inventory\\Amelia\\S') != []:  #Checks to see if the directory is empty
          S_Amelias()
  
  def Select_Chrome():
      pyautogui.hotkey('Alt', 'Tab'),  #Selects Chrome
  
  def XXS_Amelias():
      time.sleep(0.2),
      pyautogui.moveTo(1074,160),     #Moves cursor to "Add Photos"
      time.sleep(0.2),                #ZZZ
      pyautogui.click(),              #Clicks "Add Photos"
      time.sleep(1.5),                #ZZZ
      pyautogui.hotkey('Ctrl', 'l'),  #Selects Address Bar
      time.sleep(0.2),
      pyautogui.typewrite('D:\\Inventory\\Amelia\\XXS'),         #Pastes in correct directory path
      time.sleep(0.2),
      pyautogui.hotkey('Enter'),      #Hits enter to open correct directory
      pyautogui.moveTo(1000,600),     #Positions cursor in the middle of the directory window
      pyautogui.click(),              #Clicks in the middle of the directory window to select the window
      pyautogui.hotkey('Ctrl', 'a')   #Selects all
      time.sleep(0.2),                #ZZZ
      pyautogui.hotkey('Enter')       #Hits enter to upload all selected files
      time.sleep(0.5),                #ZZZ
      pyautogui.hotkey('Ctrl', 'Tab'), #Selects the Next tab
  
  def XS_Amelias():
      time.sleep(0.2),
      pyautogui.moveTo(1074,160),     #Moves cursor to "Add Photos"
      time.sleep(0.2),                #ZZZ
      pyautogui.click(),              #Clicks "Add Photos"
      time.sleep(1.5),                #ZZZ
      pyautogui.hotkey('Ctrl', 'l'),  #Selects Address Bar
      time.sleep(0.2),
      pyautogui.typewrite('D:\\Inventory\\Amelia\\XS'),         #Pastes in correct directory path
      time.sleep(0.2),
      pyautogui.hotkey('Enter'),      #Hits enter to open correct directory
      pyautogui.moveTo(1000,600),     #Positions cursor in the middle of the directory window
      pyautogui.click(),              #Clicks in the middle of the directory window to select the window
      pyautogui.hotkey('Ctrl', 'a')   #Selects all
      time.sleep(0.2),                #ZZZ
      pyautogui.hotkey('Enter')       #Hits enter to upload all selected files
      time.sleep(0.5),                #ZZZ
      pyautogui.hotkey('Ctrl', 'Tab'), #Selects the Next tab
  
  def S_Amelias():
      time.sleep(0.2),
      pyautogui.moveTo(1074,160),     #Moves cursor to "Add Photos"
      time.sleep(0.2),                #ZZZ
      pyautogui.click(),              #Clicks "Add Photos"
      time.sleep(1.5),                #ZZZ
      pyautogui.hotkey('Ctrl', 'l'),  #Selects Address Bar
      time.sleep(0.2),
      pyautogui.typewrite('D:\\Inventory\\Amelia\\S'),         #Pastes in correct directory path
      time.sleep(0.2),
      pyautogui.hotkey('Enter'),      #Hits enter to open correct directory
      pyautogui.moveTo(1000,600),     #Positions cursor in the middle of the directory window
      pyautogui.click(),              #Clicks in the middle of the directory window to select the window
      pyautogui.hotkey('Ctrl', 'a')   #Selects all
      time.sleep(0.2),                #ZZZ
      pyautogui.hotkey('Enter')       #Hits enter to upload all selected files
      time.sleep(0.5),                #ZZZ
      pyautogui.hotkey('Ctrl', 'Tab'), #Selects the Next tab
  
  button = Button(master, text = "Upload All Amelias", command=Upload_All_Amelias, height = 5, width = 20)
  
  button.pack()
  
  mainloop()
  </code></pre>
  
  <p>Again, everything seems to works perfectly when I call all of them without the if/then statements. But, for some reason, when I use them, it crashes.</p>
  
  <p>Here is the definition of Upload_All_Amelias when the code works:</p>
  
  <pre><code>def Upload_All_Amelias():
      Select_Chrome()
      time.sleep(1)
      XXS_Amelias()
      time.sleep(1)
      XS_Amelias()
      time.sleep(1)
      S_Amelias()
  </code></pre>
  
  <p>Here's the error message I get when I run it with the if/then statements included:</p>
  
  <pre><code>Exception in Tkinter callback
  Traceback (most recent call last):
    File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__
      return self.func(*args)
    File "C:/Users/Sam/Python Projects/UPLOAD_ALL_AMELIAS_5a.py", line 16, in Upload_All_Amelias
      XS_Amelias()
    File "C:/Users/Sam/Python Projects/UPLOAD_ALL_AMELIAS_5a.py", line 51, in XS_Amelias
      pyautogui.click(),              #Clicks "Add Photos"
    File "C:\Python34\lib\site-packages\pyautogui-0.9.33-py3.4.egg\pyautogui\__init__.py", line 362, in click
      platformModule._click(x, y, 'left')
    File "C:\Python34\lib\site-packages\pyautogui-0.9.33-py3.4.egg\pyautogui\_pyautogui_win.py", line 437, in _click
      _sendMouseEvent(MOUSEEVENTF_LEFTCLICK, x, y)
    File "C:\Python34\lib\site-packages\pyautogui-0.9.33-py3.4.egg\pyautogui\_pyautogui_win.py", line 480, in _sendMouseEvent
      raise ctypes.WinError()
  FileNotFoundError: [WinError 18] There are no more files.
  </code></pre>
  
  <p>One more bit of info, it gets through the first statement fine, then crashes on the second. But the sections should be identical with the exception of the directory it's calling.</p>
  
  <p>I'm sorry for such a long entry. Any help would be appreciated.</p>