Andromeda
Andromeda

Reputation: 13

Python read() function doesn't return anything

So my intention is to record all keypresses of my keyboard and store those keypresses in a notepad so i can replay them from that save file but even when wf.write() overwrites the file with the keypresses the print(data) doesnt print anything, also keyboard.play() can't play and string type variable but that's something i am still working on, by now i just want to see the read result.

import keyboard
import time
import random
import os

with open("pasos.txt",'r') as rf:
    with open("pasos.txt",'w') as wf:
        lc=keyboard.record(until="shift")
        sc=str(lc)
        wf.write(sc)
        data=rf.read()
        print(data)
        #keyboard.play(save,speed_factor=1)
``

Upvotes: 1

Views: 520

Answers (1)

Barmar
Barmar

Reputation: 780929

File output is buffered. You need to flush the output to the file so that it can be read by another stream.

with open("pasos.txt",'r') as rf:
    with open("pasos.txt",'w') as wf:
        lc=keyboard.record(until="shift")
        sc=str(lc)
        wf.write(sc)
        wr.flush()
        data=rf.read()
        print(data)
        #keyboard.play(save,speed_factor=1)

Upvotes: 1

Related Questions