'write content from a .css file to a text editor in python

Hello so in my python script im taking inputs from users and these inputs are written into parts of the css file.

    pag.moveTo(discord[0], discord[1], 0.5)
    pag.click()
    time.sleep(2)

    
    pag.moveTo(settings[0], settings[1], 0.5)
    pag.click()
    time.sleep(2)

    
    pag.moveTo(customcss[0], customcss[1], 0.5)
    pag.click()
    time.sleep(2)

    
    pag.moveTo(line1[0], line1[1], 0.5)
    pag.click()
    time.sleep(5)
    


    
    with open(name + '.css') as f:
      lines = f.readlines()


    pag.typewrite(lines)

The problem is when the code gets to typewrite the text in the .css file it just presses enter about 20 times and prints nothing from the file how can i fix this



Solution 1:[1]

The pyautogui.typewrite() function has two modes. The first mode takes a string and simulates pressing a key for each of the letters in the string. The second mode takes a list of strings, in which case it expects each string in the list to be a key name, not a character.

Here's documentation for the function:

def typewrite(message, interval=0.0, logScreenshot=None, _pause=True):

Performs a keyboard key press down, followed by a release, for each of the characters in message. The message argument can also be list of strings, in which case any valid keyboard name can be used. Since this performs a sequence of keyboard presses and does not hold down keys, it cannot be used to perform keyboard shortcuts. Use the hotkey() function for that.

Args:

message (str, list): If a string, then the characters to be pressed. If a list, then the key names of the keys to press in order. The valid names are listed in KEYBOARD_KEYS.

Because you call readlines() to read your file, lines contains a list of strings, one for each line of the file. If you instead called read(), lines would be a single string. So with:

lines = f.readlines()
...
pag.typewrite(lines)

you are passing a list to typewrite. In that case, per the above documentation, the function expects each string in lines to contain key names, not characters. I assume that the file you're reading does not contain a list of key names.

If you use f.read() instead of f.readlines(), you'll get the behavior you expect.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1