L
L
lbvfckren2020-06-08 15:56:28
Python
lbvfckren, 2020-06-08 15:56:28

How to write characters to a file?

Hello, I'm trying to make it so that after pressing a key it is immediately written to a text file
. I tried this:
import keyboard

x = open('test.txt','a')
def print_pressed_keys(e):
x.write(str( e.name))
print(e.name)

keyboard.hook(print_pressed_keys)
keyboard.wait()
The file remains empty, but the press passes and print prints it out. Tell me what to do.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-06-08
@lbvfckren

Because the file didn't close -> didn't save.

def print_pressed_keys(e):
    with open('test.txt', 'a') as f:
        f.write(str(e.name))
    print(e.name)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question