Search code examples
pyautogui

PYAUTOGUI IN FOR LOOP Python


I am trying to do a series of steps on each file in the directory. For each file, I want to copy the name of the file and then paste it at specified coordinates on the screen. The first three lines of code in the if statement seem to get ignored and all I see happen is the cursor move to the destination. None of the file names are getting copied to the clipboard and none are pasted. Please see error picture message attached. Any advice is much appreciated. pythonPicError

import pyautogui,os
directory = 'C:\\Users\\johna\\Desktop\\pdfs'
   for filename in os.listdir(directory):
     if filename.endswith('.txt'):
        pyautogui.click() 
        pyautogui.press('f2') #select file name
        pyautogui.hotkey('ctrl','c') #copy file name
        pyautogui.moveTo(153,1054,duration=2)
        pyautogui.click() #click on destination
        pyautogui.hotkey('ctrl','v') #paste file name

Solution

  • Print something after each line of code, to see where exactly is the problem. But I guess

    pyautogui.hotkey('ctrl','c') #copy file name
    

    ctrl+c is just killing your script :)

    Just noticed:

    pyautogui.click() 
    

    You didn't provide any coords for this click, so it's clicking just after you run the program in the place where your coursor is.