I am trying to test a simple code that reads a file line-by-line with Pycharm.

for line in sys.stdin:
    name, _ = line.strip().split("\t")
    print name

I have the file I want to input in the same directory: lib.txt

How can I debug my code in Pycharm with the input file?

有帮助吗?

解决方案 3

You need to create a custom run configuration and then add your file as an argument in the "Script Parameters" box. See Pycharm's online help for a step-by-step guide.

However, even if you do that (as you have discovered), your problem won't work since you aren't parsing the correct command line arguments.

You need to instead use argparse:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("filename", help="The filename to be processed")
args = parser.parse_args()
if args.filename:
    with open(filename) as f:    
        for line in f:
            name, _ = line.strip().split('\t')
            print(name)

其他提示

You can work around this issue if you use the fileinput module rather than trying to read stdin directly.

With fileinput, if the script receives a filename(s) in the arguments, it will read from the arguments in order. In your case, replace your code above with:

import fileinput
for line in fileinput.input():
    name, _ = line.strip().split("\t")
    print name

The great thing about fileinput is that it defaults to stdin if no arguments are supplied (or if the argument '-' is supplied).

Now you can create a run configuration and supply the filename of the file you want to use as stdin as the sole argument to your script.

Read more about fileinput here

I have been trying to find a way to use reading file as stdin in PyCharm. However, most of guys including jet brains said that there is no way and no support, it is the feature of command line which is not related PyCharm itself. * https://intellij-support.jetbrains.com/hc/en-us/community/posts/206588305-How-to-redirect-standard-input-output-inside-PyCharm-

Actually, this feature, reading file as stdin, is somehow essential for me to ease giving inputs to solve a programming problem from hackerank or acmicpc.

I found a simple way. I can use input() to give stdin from file as well!

import sys
sys.stdin = open('input.in', 'r')
sys.stdout = open('output.out', 'w')

print(input())
print(input())

input.in example:

hello world
This is not the world ever I have known

output.out example:

hello world
This is not the world ever I have known

For flexibility, you can write your python script to always read from stdin and then use command redirection to read from a file:

$ python myscript.py < file.txt

However, as far as I can tell, you cannot use redirection from PyCharm as Run Configuration does not allow it.

Alternatively, you can accept the file name as a command-line argument:

$ python myscript.py file.txt

There are several ways to deal with this. I think argparse is overkill for this situation. Alternatively, you can access command-line arguments directly with sys.argv:

import sys

filename = sys.argv[1]
with open(filename) as f:    
    for line in f:
        name, _ = line.strip().split('\t')
        print(name)

For robust code, you can check that the correct number of arguments are given.

Here's my hack for google code jam today, wish me luck. Idea is to comment out monkey() before submitting:

def monkey():
    print('Warning, monkey patching')
    global input
    input = iter(open('in.txt')).next

monkey()

T = int(input())

for caseNum in range(1, T + 1):
    N, L = list(map(int, input().split()))
    nums = list(map(int, input().split()))

edit for python3:

def monkey():
    print('Warning, monkey patching')
    global input
    it = iter(open('in.txt'))
    input = lambda : next(it)

monkey()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top