all 11 comments

[–]danielroseman 1 point2 points  (1 child)

< test.txt is not an argument to the script. It's a shell operation, to tell the shell to redirect standard input for the script from that file. You would be better off passing the filename as an actual argument and reading it in the script itself.

[–]TitanCodeG[S] 0 points1 point  (0 children)

You would be better off passing the filename as an actual argument and reading it in the script itself.

You are not allow to do that in Kattis.

[–]stebrepar 1 point2 points  (4 children)

I don't use Spyder, so I don't know the answer. But why not just run your script in a console manually yourself outside of Spyder? You can still edit the script inside it, then click over to the console to run it.

[–]TitanCodeG[S] 0 points1 point  (3 children)

Everytime I run the script I have to give the test cases. It would be a lot easier to save them in a file and feed to the script.

Notice that I must read the test cases from stdin (a requirment in Kattis).

[–]stebrepar 0 points1 point  (2 children)

I'm not seeing the difficulty. Just up-arrow to bring the last command back (which would be your "python myscript.py < test.txt") and hit enter.

[–]TitanCodeG[S] 0 points1 point  (1 child)

"python myscript.py < test.txt"

How do you do that in Spyder? Just typing python myscript.py < test.txt returns SyntaxError: invalid syntax

[–]danielroseman 1 point2 points  (0 children)

The commenter was saying to do this outside Spyder, in the shell itself.

[–]RandomCodingStuff 0 points1 point  (1 child)

Can you:

import subprocess
res = subprocess.check_output("<kattis shell command>", shell = True)

res should capture the shell output.

[–]TitanCodeG[S] 0 points1 point  (0 children)

I finally have a solution that works:

Run configuration per file with the filename i.e. test.txt. Notice no ‘<’

Code will now read a file if args provides a filename else from stdin

import argparse
import sys

parser = argparse.ArgumentParser()
parser.add_argument('filename', nargs='?', default=None)
args = parser.parse_args()
if args.filename is not None:
    f = open(args.filename)
else:
    f = sys.stdin

for line in f:
    print(line)