How do I redirect input and output with PyCharm like I would on the command line?
Redirect input file to stdin
You can load content to stdin by using StringIO
import StringIO
sys.stdin = StringIO.StringIO('your input')
So if you want redirect input from file, you can read data from file and load it to stdin
import StringIO
input = "".join(open("input_file", "r").readlines())
sys.stdin = StringIO.StringIO(input)
In case you want to take filename as first system argument
import StringIO
filename = sys.argv[1]
input = "".join(open("input_file", "r").readlines())
sys.stdin = StringIO.StringIO(input)
Screenshots:
Main Program
Debug Configruration
Redirect stdout to output file
Similar idea as below sample code
import sys
sys.stdout = open(outputFile, mode='w', buffering=0)
#skip 'buffering' if you don't want the output to be flushed right away after written
(added in Pycharm 5)
In the Edit Configurations
screen under Logs
tab check the option:
Save console output to file
and provide a FULL PATH to the outputfile.
thats it - works like magic