简体   繁体   中英

(Python 3 | LINUX) Is there a way to use termios.tcflush to flush inputs from a file?

I have created a program that simulates a person typing. I am getting the error: termios.error: (25, 'Inappropriate ioctl for device') I think this is happening because I am giving a text file as input to the code. Here are the libraries I am using and some code:

from pynput.keyboard import Key, Controller
from termios import tcflush, TCIFLUSH
from sys import stdin, stdout
from time import sleep
from random import randint

keyboard = Controller()

def typeS(self, string):
    for i, character in enumerate(string):
        try:
            self.press(character)
            sleep(randint(1, 3) * 0.07)
            self.release(character)
            sleep(randint(1, 3) * 0.07)
        except ValueError:
            raise self.InvalidCharacterException(i, character)

word = input()
timings = input()

word = word.split(",")
timings = timings.split(",")

print(f"word = {word}")
print(f"timings = {timings}")

typeS(keyboard, word)

tcflush(stdin, TCIFLUSH)

and the command line command I am using: python3 faketyper.py < fakeprofile-1.txt I need to use tcflush so that when the code presses a key, the stored things are not later printed to the command line. Ignore any syntax or errors you see in the code because I did not copy and paste every single line. The main goal is for me to find out how to use tcflush (OR SOMETHING ELSE) to flush the Input so that nothing is printed out to the command line after the code is done running.

Answered in the comments. Simply change "stdin" to "stdout." Shout out to Tim Roberts for his insight.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM