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


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


8 Answers

New Answer


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 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.


Sergej If sounds like you want to mock the input function. Check out this link for options to consider:


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.


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


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, where I 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.


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


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.