#🔒 How to give inputs to input() and read the results of print() via another file?

13 messages · Page 1 of 1 (latest)

somber summit
#

Hello, so I asked some of my friends to do a project so they can get familiar with python. In their projects they will get some inputs with input() and give an output with print(). I want to write a tester file, with which I can give automatic inputs and check if their outputs are correct. However, idk how I can read the stdout and give inputs as stdin through another file. Does anyone have any idea on how I can do this?

austere phoenixBOT
#

@somber summit

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

silent shell
#

!d sys.stdout

austere phoenixBOT
#

sys.stdin``````py

sys.stdout``````py

sys.stderr```
[File objects](https://docs.python.org/3/glossary.html#term-file-object) used by the interpreter for standard input, output and errors:

• `stdin` is used for all interactive input (including calls to [`input()`](https://docs.python.org/3/library/functions.html#input));

• `stdout` is used for the output of [`print()`](https://docs.python.org/3/library/functions.html#print) and [expression](https://docs.python.org/3/glossary.html#term-expression) statements and for the prompts of [`input()`](https://docs.python.org/3/library/functions.html#input);

• The interpreter’s own prompts and its error messages go to `stderr`.

These streams are regular [text files](https://docs.python.org/3/glossary.html#term-text-file) like those returned by the [`open()`](https://docs.python.org/3/library/functions.html#open) function. Their parameters are chosen as follows:
chilly fossil
#

!d unittest

austere phoenixBOT
#

Source code: Lib/unittest/__init__.py

(If you are already familiar with the basic concepts of testing, you might want to skip to the list of assert methods.)

The unittest unit testing framework was originally inspired by JUnit and has a similar flavor as major unit testing frameworks in other languages. It supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework.

To achieve this, unittest supports some important concepts in an object-oriented way:

bleak basalt
#

Just assign the input() to a variable and write it to a file after??

#

i = input()
file.write(i)

olive wolf
#

Have a look at this for unittest: https://stackoverflow.com/a/69487110

You can alternatively do this:

from subprocess import Popen, PIPE

p = Popen("path_to_file.exe", stdout=PIPE, stdin=PIPE, stderr=PIPE, text=True)
stdout_data = p.communicate(input="test input")[0]
print(stdout_data)

You'll have to convert your friends script to an executable in order for the above to work.
You can use pyinstaller for that:
pyinstaller --onefile path_to_script.py
The executable would be located in the created dist directory.

somber summit
#

Thank you everyone for the help. I think I got it now

somber summit
#

!close

austere phoenixBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.