With the command line if I am running a python file I can enter:
python filename.py < filename.in > filename.out
Is there a way to mimic this behavior in PyCharm?
With the command line if I am running a python file I can enter:
python filename.py < filename.in > filename.out
Is there a way to mimic this behavior in PyCharm?
(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 a py-charm
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
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
As for the input, you can provide space separated input parameters in Script parameters
field under Run->Edit Configurations
. There's no direct way in pyCharm to redirect the output to a file unless you are using some wrapper class whose sole job is to write the wrapped module's output to a file.
In PyCharm 5 (or even previous versions), you can do this by modifying the script parameters in Edit Configurations window. In that box, write
< filename.in
> filename.out
on separate lines
Since I couldn't put any loading code in the python file, the solution for me was to:
Create file launcher.sh
with content:
python filename.py < filename.in > filename.out
In PyCharm create bash configuration: Run -> Edit Configurations -> + Bash
and put launcher.sh
as Script name
I'm not sure why this was not accepted / working , but in PyCharm 2017 the following approach works:
In the Run/Debug Configuration
window , open the Script Parameters
dialog and enter your input and/or output files on separate lines like this ( with quotes ):
< "input01.txt"
> "output01.txt"
Notice that I have >>
here , this appends the output to output01.txt
so that I have it all over multiple runs .
I don't see why this approach wouldn't work with older versions of PyCharm as it executes the following line using this configuration:
Also this approach works with a remote interpreter on a Vagrant instance , which is why that command is using ssh .