0

Hi I have a very naive question.

I have this piece of python code test.py that I am trying to test run. May I know how do I run it using my own inputs?

I tried running it on my command line using python3 test.py ABCD but the command line does not return anything..

I have also tried python3 test.py ABCD.txt where ABCD.txtcontains the line ABCD but also to no avail...

Any help would be greatly appreciated, thank you.

import sys
output = []
for ln in sys.stdin:
   for c in ln:
      if c in 'ABCD':
           output.append(c)
sys.stdout.write(''.join(output) + '\n')
2
  • 3
    use python3 test.py < ABCD.txt Commented Aug 31, 2022 at 16:02
  • Or python3 test.py <<<'ABCD', if you only want one line of input. Or echo ABCD | python3 test.py, or... etc, etc etc. Commented Aug 31, 2022 at 16:19

2 Answers 2

1

python3 test.py < ABCD.txt as suggested by keith works, thank you everyone!

Sign up to request clarification or add additional context in comments.

Comments

0

Running the code like python code.py a b c d will result in storing a, b, c, and d as input command line arguments. So to handle that you need to use sys.argv and edit the code as follows.

import sys
output = []
for ln in sys.argv:
   for c in ln:
      if c in 'ABCD':
           output.append(c)
sys.stdout.write(''.join(output) + '\n')

check here for more info.

However, all standard inputs to an application are handled using < command. Just like the standard output which is handled by >. So to run the just add < like :python3 test.py < ABCD and it should be ok.

2 Comments

Modifying the code may not be an option. The question is how to provide standard input, not how to avoid reading standard input.
thank you for the suggestion! ya unfortunately modifying the code is not an option, is there a way to run this code without changing it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.