I want to write and test scripts for Kattis problems in Spyder. In Kattis problems a file with the test inputs are feed to the script. Like python myscript.py < test.txt
.
I want to run my script in Spyder feeding the test inputs. How do I do that?
I used to be able to run a script in Spyder and specify command line arguments. Now, the only way this can be done is if the script is run in an external console. This console then closes as soon as the script finishes, so you can't see the output of your script.
I used to be able to set command line options in the General settings. (Like explained here: https://github.com/spyder-ide/spyder/issues/7292) But that seems not to be possible anymore.
I also tried runfile('myscript.py', args = '< test.txt')
But the args = '< test.txt'
seems to be ignored.
Opening a file in the script is not an option. The test cases must be read from stdin in Kattis problems.
How do I pass command line arguments / feed a text file to stdin?
Edit: Tried Ctrl + F6
setting command line arguments in box. '< test.txt'
The file gets ignored. Script waits for input in console.
Tried runfile('myscript.py', args = '< test.txt')
Result: The file gets ignored.
I am using Spyder 5.5.0
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)