How pass arguments from a text file to python script thru input() method?? | SoloLearn: Learn to code for FREE!

+3

How pass arguments from a text file to python script thru input() method??

I want to test my python scripts for a lot of inputs to see how it handles, but I came across a problem where I have to manually enter values for some variables in a script thrue input() statements in the script. I don't want to change my script i.e. I want to leave those input() statements. Is there any way to pass values stored in a separate file to a python script thru input() method. Thx, <3

12/3/2020 11:37:06 PM

Sergej

8 Answers

New Answer

+3

Sergej maybe this will help. In shell scripts you can redirect input to come from a text file by using the < operator on the command line. Example: myprogram <inputs.txt This will run myprogram and read inputs.txt as the input source instead of the console. Also, console output can be redirected to a text file by using the > operator.

+7

https://code.sololearn.com/ckFzz7miBtn1/?ref=app If sololearn codeplayground asks for input then just hit "submit"-button. that's just a bug in sololearn code playground. it works well in pydroid3.

+5

Sergej If sounds like you want to mock the input function. Check out this link for options to consider: https://stackoverflow.com/questions/21046717/python-mocking-raw-input-in-unittests

+3

Sergej Are you looking to use your file as an input source for testing purposes? Are you familiar with the concept of unit tests? What you are asking is aligned with the practice of unit testing and TDD (Test Driven Development). The idea involves writing functions that follows conventions allowing for the code to be testable. Such conventions involve concepts known as mocking / fakes, dependency injection, and spies. Another option is integration tests which simulate user interaction via test automation. This might involve files as input sources that are loaded by a test runner, used as input, and evaluating the results. I guess to a learner, these might be advanced topics. If you aren't studying test automation, then you might be looking for something else all together.

+2

A for loop might work, but I don't know, would have to see the code.

+2

Thank you all for answers, but I think I formed my question wrong. So here is my real problem: I made a simple learning app in python (in tKinter, but doesn't matter) for my school project. It is the most basic version of let say CodeSignal. Now in my question I prompt my user to use input() in each line where it says in question it will pass values, e.g. "Input data are two non-negative integers given in two lines. In one and only output line print their sum!". And now I save their script and run my tester.py, where I subprocess.call() to run it and get output but of course it waits for user input on those input statements and outputs anything. I was wondering is there any way to input values from ,idk .json or .txt, files in such script in shell.

+2

Brian that is exactly what I was looking for. Thank you!!!

+1

David Carroll, thank you sir, I really appreciate it. I've never heard of unit testing or TDD, but I'll give a shoot and see if it suits my code. Unfortunately, I was hopping that there was something much easier and faster to implement, because I've seen thing likes this be done in c++, myb there is also that option in python, but I just didn't describe my problem well. Nevertheless, thank you.