39

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?

Sophie
  • 495
  • 1
  • 4
  • 9
  • You should pick up Vu Anh answer as accepted one https://stackoverflow.com/a/39482389/248616 – Nam G VU Aug 03 '17 at 02:19
  • @NamGVU in new python 2018.3.4 you can set "redirect input from:" in the debugger PyCharm settings and the output can be redirected as presented below by yossico, stackoverflow.com/a/39482389/248616 modifies the original script which is not intended, – Wojciech Sobczyk May 03 '19 at 08:17

6 Answers6

46

(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

enter image description here

Federico Baù
  • 6,013
  • 5
  • 30
  • 38
yossico
  • 3,421
  • 5
  • 41
  • 76
5

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
Hagai
  • 678
  • 7
  • 20
Vu Anh
  • 955
  • 1
  • 18
  • 29
  • You're suggesting to change the program to read input from a file. The question was about managing this "from the outside"; see the other answers about the Run-configurations. – Volker Stolz Nov 16 '22 at 10:11
4

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.

Shan Valleru
  • 3,093
  • 1
  • 22
  • 21
3

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

Nitin
  • 7,187
  • 6
  • 31
  • 36
  • @yossico You are right. I do not see this option any more in the current version of PyCharm. Sorry. – Nitin Mar 04 '16 at 05:24
1

Since I couldn't put any loading code in the python file, the solution for me was to:

  1. Install BashSupport plugin
  2. Create file launcher.sh with content:

    python filename.py < filename.in > filename.out

  3. In PyCharm create bash configuration: Run -> Edit Configurations -> + Bash and put launcher.sh as Script name

Jiří Mauritz
  • 421
  • 1
  • 4
  • 11
-1

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"

script parameters dialog

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: PyCharm Command Line

Also this approach works with a remote interpreter on a Vagrant instance , which is why that command is using ssh .

LostNomad311
  • 1,975
  • 2
  • 23
  • 31
  • I'm using PyCharm 2017.2 and this is not working for me. – Nam G VU Aug 03 '17 at 02:08
  • 1
    you'll have to give specifics ...try copy pasting what I have there , then change the file names inside the quotes . also be sure those file paths are correct ( at least the input ) . – LostNomad311 Aug 03 '17 at 18:41
  • I guess I missed the quote. Will try again and back to us later – Nam G VU Aug 04 '17 at 00:16
  • I've just tried and it's not working in PyCharm 2017.2 - my sample code to use is here https://github.com/namgivu/hackerrank/tree/master/_archive_/input/170802_1617_input_sample_faster – Nam G VU Aug 04 '17 at 01:55
  • that looks correct , how do you know it isn't working ? is there an error message ? you have a lot of duplicate run configs , are you sure you're running the correct one ? – LostNomad311 Aug 04 '17 at 22:22
  • I start the debug to see if I can read from input file instead of entering values thru the console – Nam G VU Aug 05 '17 at 00:49
  • Of course I am sure what I am doing. I right clicking the .py file to start the debug – Nam G VU Aug 05 '17 at 00:50