Answer the question
In order to leave comments, you need to log in
How to write data to a file in python?
def on_press(key):
l = '{0}'.format(key)
with Listener(
on_press=on_press) as listener:
listener.join()
f = open("log.txt", "w")
f.write(l)
f.close()
Answer the question
In order to leave comments, you need to log in
First, you need to format the code with a special tag.
Secondly, your variable l is local and not visible outside the function.
Thirdly, to write something to a file, it is better to use this construction:
with open("log.txt", "w") as f:
f.write('any text')
open('filename.log', 'a')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question