0

I have a need to fill stdin from code directly when input() is waiting for filling. Is there to do the next:

# Here suppose to be some code that will automatically fill input() below
string = input("Input something: ")
# Or here

I've heard about subprocess.Popen, but I don't understand how to use it in my case. Thank you.

  • Does this answer your question? [Is it possible to prefill a input() in Python 3's Command Line Interface?](https://stackoverflow.com/questions/8505163/is-it-possible-to-prefill-a-input-in-python-3s-command-line-interface) – JonSG Mar 15 '22 at 19:42
  • @JonSG, no, I need to automate completely this process. No" backspace" and "enter" clicking. –  Mar 15 '22 at 20:11

1 Answers1

0

This code is something:

import sys
from io import StringIO


class File(StringIO):
    def __init__(self):
        self._origin_out = sys.stdout
        self._origin_in = sys.stdin
        sys.stdout = self
        sys.stdin = self
        self._in_data = ''
        super(File, self).__init__()

    def write(self, data):
        if data == 'My name is:':
            self._in_data = 'Vasja\n'
        else:
            self._origin_out.write(data)

    def readline(self, *args, **kwargs):
        res = self._in_data
        if res:
            self._in_data = ''
            return res
        else:
            return sys.stdin.readline(*args, **kwargs)

    def __del__(self):
        sys.stdout = self._origin_out
        sys.stdin = self._origin_in


global_out_file = File()

a = input('My name is:')
print('Entered name is:', a)