Skip to content

Instantly share code, notes, and snippets.

@pokk
Last active July 23, 2018 06:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pokk/1c0c265204ee913bc958c04a5c8e0e4a to your computer and use it in GitHub Desktop.
Save pokk/1c0c265204ee913bc958c04a5c8e0e4a to your computer and use it in GitHub Desktop.
Redirect file stdin in PyCharm

Introduction

For Pycharm IDE, we want to use redirect input file while running the code.

How to Use

  1. You can make a file for a input data as like *.in.
  2. Typing auto_redirect_input_file.py in top of code.
  3. Click the "Edit Configuration".
  4. Put the file name *.in in "Script parameters".
  5. And, no and. Enjoy it!!

Talk is cheap. Show me the code.

import io
import sys

# Simulate the redirect stdin.
if len(sys.argv) > 1:
    filename = sys.argv[1]
    inp = ''.join(open(filename, "r").readlines())
    sys.stdin = io.StringIO(inp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment