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 underLogs
tab check the option:Save console output to file
and provide a FULL PATH to the outputfile. thats it - works like magicSince 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 putlauncher.sh
as Script nameI'm not sure why this was not accepted / working , but in PyCharm 2017 the following approach works:
In the
Run/Debug Configuration
window , open theScript Parameters
dialog and enter your input and/or output files on separate lines like this ( with quotes ):Notice that I have
>>
here , this appends the output tooutput01.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 .
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
As for the input, you can provide space separated input parameters in
Script parameters
field underRun->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.Redirect input file to stdin
You can load content to stdin by using
StringIO
So if you want redirect input from file, you can read data from file and load it to stdin
In case you want to take filename as first system argument
Screenshots:
Main Program
Debug Configruration
Redirect stdout to output file
Similar idea as below sample code